awoo's blog

By awoo, history, 6 years ago, In English

1027A - Palindromic Twist

Tutorial
Solution (PikMike)

1027B - Numbers on the Chessboard

Tutorial
Solution (Vovuh)

1027C - Minimum Value Rectangle

Tutorial
Solution (PikMike)

1027D - Mouse Hunt

Tutorial
Solution (adedalic)

1027E - Inverse Coloring

Tutorial
Solution (PikMike) O(n^3)
Solution (BledDest) O(n^2)

1027F - Session in BSU

Tutorial
Solution (Vovuh)
Solution (Vovuh) Kuhn's Algorithm

1027G - X-mouse in the Campus

Tutorial
Solution (adedalic)
  • Vote: I like it
  • +72
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it +67 Vote: I do not like it

You don't need a binary search in problem F. The complexity is O(nlogn) for coordinate compression. Build the graph, then apply DSU or SCC(Tarjan or Korasaju) with do the job. If there exists a connected component with number of edges greater than the number of vertices, then the answer is -1. If the number of edges is equal to the number of vertices, record the largest value, If the number of edges is equal to the number of vertices minus one, record the second largest value, the answer is the maximum over all connected components.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I was trying the same way but got tle in 45th test case. Any optimization you would suggest? I have used path compression and union by rank.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      You were using map in your program, which adds an additional log factor together with large constant.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can you please elaborate on the part of partial sums in problem E to solve the problem in N^2 time. I am unable to get it.

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it +37 Vote: I do not like it

    let lte[i] be the n.of binary strings of length n such that maximum segment of a single color has length <= i. (lte[i]-lte[i-1]) will be the n.of binary strings of length n such that maximum segment of a single color has length = i. Hence if we have lte array we can solve the problem. We can calculate lte[i] in O(n) as follows. let dp[j] be the n.of binary strings of length j such that maximum segment of a single color has length <= i.

    If j>i dp[j]=dp[j-1]+dp[j-2]....dp[j-i]

    else dp[j]=dp[j-1]+dp[j-2]..+dp[1]+1.

    This is because in a string of length j at the beginning we can have at most i bits with the same color and after that, it's just dp[remaining length]. Now, lte[i]=dp[n]. Clearly, we can calculate the dp array by using partial sums in O(n). Since each element of the lte array takes O(n) time to calculate overall complexity is O(n^2). P.S: Don't forget to multiply by 2 at the end since all this is for a fixed color of the top left tile and it has 2 options (white or black).

    AC: O(n^2)

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks a lot.

      Can you just tell me why are we adding 1 in case of j <= i.

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        I think I got it. You are assuming that 1st element is fixed and 1 is for assuming all the j elements are same. Thanks.

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it +21 Vote: I do not like it

          yes, glad that I was of some help :D

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Can you please explain the relation for j>i

      dp[j] = dp[j-1]+...dp[j-i]

      How ?

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      This is because in a string of length j at the beginning we can have at most i bits with the same color and after that, it's just dp[remaining length].

      I don't see why the reasoning is correct.

      If you put X bit of same color in the beginning and fill the rest with dp[j — X] then it is possible to end up with a segment of same color that has length more than i. isn't it?

      I guess you meant something else by beginning.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone find out what is time complexity on my code? (problem C)

Here is my code 41811657 I think it should be O(T*n*log(n)) . But why it got TLE in case 6. Thank you!!

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Maybe because ?

    In fact, for each test you create arrays a and cnt istead of making it global and clearing after each test.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Why is this getting tle sir??please help me 41788605

      • »
        »
        »
        »
        6 years ago, # ^ |
        Rev. 4   Vote: I like it +1 Vote: I do not like it

        Try using ios_base::sync_with_stdio(false). I optimized my solution from TLE to AC.

        TLE on test 6: 41767762

        AC: 41767854

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Wow, that helped me too. Thank you!

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Try scanf and printf rather than cin and cout it worked for me

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          but ,will i be able to use sort() function , if i am taking input through scanf() ,because i have coded in c++

          • »
            »
            »
            »
            »
            »
            6 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Ya Of course

            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              This code is not working , dude can you figure out the error

              #include<bits/stdc++.h>
              main()
              {
                 int i,arr[5];
                 for( i=0;i<5;i++)
                 {
                    arr[i]=5-i;
                 }
                  sort(arr,arr+5);
                 for(i=0;i<5;i++)
                 {
                    printf("%d  ",arr[i]);
                 }
              }
              
»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

What does this line do in the code for Problem E? ans = (ans * (long long)((MOD + 1) / 2)) % MOD; Edit : Understood. For anyone having trouble it is modular inverse

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    It divides ans by 2 modulo MOD, as is the modular inverse of 2, because

»
6 years ago, # |
Rev. 3   Vote: I like it +13 Vote: I do not like it

Kuhn's algo for F works only with this particular modification found in your solution -- you first check all the edges and if you find a suitable one, then you use it. Only if there are no suitable edges found, then you do a second loop and recurse into them (DFS). This versions passes tests. I don't know why.

However, a "vanilla Kuhn" that just uses a regular DFS (tries every edge to a non-visited vertex recursively) receives TLE, which is kind of expected, since it has complexity O(N*M) -- too big for this problem.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I've sent a solution for C (after the end of the contest) which sounds precisely like the given here (41817149), but it still didn't fit into given time :( What did I do wrong?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem D, Why it is better to put trap on cycle, although there may be cheaper way to put trap on each path leads to this cycle like that. https://drive.google.com/open?id=19ff-GgeGhbhGXh5MykElhYXB3EErpq_9

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Because we don't know where the mouse starts (it may start in any vertex). Mouse can be on a cycle at the beginning and if the mouse is on a cycle at the beginning, it will be on this cycle forever. Thus it is necessary to put a trap on cycle, otherwise mouse beginning on cycle will never be caught. And according to your example, if we put a trap in vertex with cost 100, mouse will go through this vertex regardless of its beginning vertex, so it is enough to put there a trap.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

As far I know Complexity of Kuhn is O(n * n). How F is Solved with Kuhn? What's the trick or am I wrong? Thanks in advance.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Kuhn works O(n*m). But tests for this assume prix is rough estimate. In real kuhn works very fast. There are also different heuristics.

»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Problem G: How to prove there is exactly u that u * x = v?

Help me. Thanks.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone help me in visualization of problem E dp.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

A little help on problem C! I don't know what I am doing wrong. I have a code in Python similar to the above solution, and I got TLE on test 5. Here is my code : http://codeforces.me/contest/1027/submission/41941233 Can someone find out?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I don't know why it gets TLE, but it also seems incorrect. Test:

    1
    7
    1 1 999 999 7 7 7
    

    Your output:

    7 7 7 7
    

    Trace:

    1) c == 1, a[i] == 7 -> c := 7

    2) c == 7, a[i] == 7 -> push

    3) c == 7, a[i] == 7 -> push

    But in fact you don't have 2 pairs

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem D, the editorial says that the mouse will stuck in a cycle. But my question is that, should the vertex in the cycle be reachable from all other nodes which are not part of the vertex's cycle because the girls are not aware of the position of the mouse? If it is required the how can we find it?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It will always be stuck in a cycle, once it reached that cycle. So all you have to do is find all cycles, take the minimum number from each cycle and add to the answer :)

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone please provide links to understand kuhn's algo?

»
6 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Can anyone help me find the complexity of my code..?for Problem D...THis is my code in java.. http://codeforces.me/contest/1027/submission/42027693 Thanks

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

sorry ignoring my algorithm (i mean whether is it correct or not) why i am getting TLE on test 2 ty 42082971

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Try to convert the input/output from cin/cout to scanf/printf and don't forget to extend the size of array ar to not get RTE.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry i tryed to change them however i dont know why it is printing wrong answers! 42083838 amazing thing about it is that when i compiled it in my computer it printed right answers!!

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem D:

Why does the mouse jump on a cycle at some point, no matter the starting vertex ?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Because it cant jump infinite times. In some number of jumps it will return to where it was

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

https://codeforces.me/contest/1027/submission/41811657 can someone tell me why I am getting tle my method is almost same as that it tutotrial

»
5 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Why do I get WA on test 3 (problem E)? https://codeforces.me/contest/1027/submission/78959890
UPD: fixed, a -1 was missing (AC: https://codeforces.me/contest/1027/submission/78974251)