wrg0ababd's blog

By wrg0ababd, history, 8 years ago, translation, In English
Tutorial is loading...
Tutorial is loading...

Simple implementation of solution described higher: http://ideone.com/IoSts1

Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
  • Vote: I like it
  • +145
  • Vote: I do not like it

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

Auto comment: topic has been updated by wrg0ababd (previous revision, new revision, compare).

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

I think Problem D constraints are very bad, because O((n + m) * n ^ 2 * k) solutions passed, but there is O((n + m) * n * k) solution...

»
8 years ago, # |
  Vote: I like it +45 Vote: I do not like it

and using intervals instead of segments

What is the difference between intervals and segments?

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

    I think segment contains it's borders, interval doesn't

  • »
    »
    8 years ago, # ^ |
      Vote: I like it -35 Vote: I do not like it

    segments are empty if l > r

    but intervals are empty if l >= r

»
8 years ago, # |
  Vote: I like it +11 Vote: I do not like it

For Problem C, making the value of eps more precise changes verdict from WA to AC. But, aren't we supposed to take any difference < 1e-6 as zero, as it is mentioned in the problem statement as well, that even the judge will compare the answers with error limit = 1e-6?

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

    It's not fair, i think, but in statement it says that it compares answers, but not anything else, printing -1 should be very precise :/

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

    The problem is there was a case (40) where using a large epsilon would give -1 but the answer was actually 0.99998999989999903804, so the output is completely off from the answer.

»
8 years ago, # |
  Vote: I like it +25 Vote: I do not like it

I thought that there has to be an equal number of vertices instead of leaves in E. The renaming of "leaves" and "vertices not being leaves" to "offices" and "departments" was strange. ;_;

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

    Problems were confusing to be honest, I fell for it too.

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

    Same! I get that they try to wrap the problem in something so that it doesn't seem boring, but clarity should be the #1 priority always.

    It's a shame as well, I really liked the problem otherwise.

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

I tried the following for problem C:

  1. Find a time Ti for each mice i in which the mice will be in the maze, using ternary search.
  2. Use binary search to find the lower and upper bound of the time, using Ti
  3. Do the intersection of the bounds and take the smallest time.

But I got WA on test 30 because of the precision error. Can you find where I did the mistake? and How to find a good EPS which will meet the requirement.

Submission: http://codeforces.me/contest/793/submission/26625111

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

here is my Solution for B i used almost exactly what you mentioned first (naive recursive). passed pretests but got TLE on test 10.

adding the below code passes test 10 but TLE on 11. any help please?

if(turns==2 && (dir=='u' || dir=='d') && y!=sj) return;
if(turns==2 && (dir=='l' || dir=='r') && x!=si) return;

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

    26615391 : same solution. TLE on test 10.

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

    My problem was TLE, and it's solved by using a boolean visiting 4D array to mark the position I have already checked with the direction and number of turns that I have checked the position with.

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

      Your solution is 90% identical to mine what a coincidence :D however i didnt use the visited array and still it worked in less time even thought im using java, i dont think u need the visited array maybe u have to recheck ur base case conditions or the recursive function , or maybe stop it when u have done already 2 turns and ure not the same line or column of the destination, cause in that case ull need one more turn to get to dest, so stop it from the beginning when turns=2 and Tx!=x and Ty!=Y

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

        Here is my code 26655398. I have already put these return cases you mentioned. However, I'll back to this problem soon, thank you:D. Edit: please look back to my previous comment, direction instead of destination, sorry for this mistake.

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

It seems to me C is in something easier to solve this way(sorry if someone have alredy written about this): We used bisection method for minimal time calculate. Let start with L=0, R=10^11. Let us now be at time M = (L + R) / 2. Then it is easy to calculate for every mouse her coordinates Xi and Yi. Obviously when Yi >= Y2 and her y-speed >= 0, we must move the right border, since she will never be inside a rectangle. Similarly examine the three other cases:

Yi <= Y1 and y-speed <= 0

Xi <= X1 and x-speed <= 0

Xi >= X2 and x-speed >= 0

And we must move the right border, if all mouses are in the rectangle. If these conditions are not met, move the left border. When we have done all iterations, we have right border as an answer. We will get it, if all mouses were in the rectangle at the same time. Else deduce -1

»
8 years ago, # |
  Vote: I like it +8 Vote: I do not like it

B has easier solution. Paint crosses on S and T with different colors (say 's' and 't'), then examine each line and each column if any of them contains /S\.*T/ or /T\.*S/ (c++26615732).

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

    Awesome idea! Thank for your share!

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

      Similar idea:
      1. Paint 's' to the left and to the right from 'S'. Same with 'T'.
      2. Rotate whole field.
      3. Repeat step "1".
      4. Try to find S+dots+T or T+dots+S (ignoring case).
      5. Repeat step "2".
      6. Repeat step "4".
      Implementation in perl (26638162).

»
8 years ago, # |
  Vote: I like it +28 Vote: I do not like it

Why do you give him so many downvotes?After all he prepared the round and posted the editorial!

»
8 years ago, # |
  Vote: I like it +17 Vote: I do not like it

The post is hidden because of too negative feedback, click here to view it

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

Same code shows two different verdict why?

Accepted code: Wrong Answer code:

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

    The code works fine with C++14 but not with C++11 but I don't know why, maybe it's a precision-related issue.

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

      Hmm ,THis is problem with Language c++11 and c++14

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

For problem B, I got WA on test 62. Here is my submission: 26615111

Can anybody help me ? What's wrong in my code ?

Update: I have found my bug. I was using visited array as vis[row][col][mov], using visvis[row][col][dir][mov] got AC. Here is my AC code: 26632674

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

I have written dfs solution for Problem B but it has got WA for test case 24.

My Code
  • »
    »
    8 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    you can visit cell before but with more turns, you need to add number of turns in used. 26637049.

»
8 years ago, # |
  Vote: I like it -18 Vote: I do not like it

I don't like this round...

»
8 years ago, # |
  Vote: I like it -11 Vote: I do not like it

In problem B, I used DP to get the minimum number of direction changes to reach 'T' from a given cell.

The DP state is: (row, column, current direction).

If the starting cell can reach 'T' in 2 direction changes or less, then the answer is YES.

This code gives me WA.

Is there a logical problem with my method?

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

    You essentially do a DFS instead of a BFS, so when you find a path, it is not necessarily the shortest one.

    In other words, such a dynamic programming solution should care about the order of visiting the cells, and yours doesn't.

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

How can problem B be solved just using 3(currx,curry,dir) states in case of bfs whereas for recursive dfs I need 4 states (currx,curry,dir,turn). I couldn't understand this part can someone explain it to me . Thank you

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

    Using 0 - 1bfs, a bfs on a graph where the edges weights are 0 or 1. This BFS instead of using a queue, uses a deque (double ended queue).

    If you change the direction, then this edge has weight 1, otherwise it has weight 0. If you go through an edge with weight 1, push it to the end of the deque, otherwise push it to the front.

    While doing the BFS, always pop the front of the deque

»
8 years ago, # |
  Vote: I like it +8 Vote: I do not like it

For Problem G,could anyone please tell me the detailed way to cut the field into O(n) rectangles without deleted cells? I can't understand the way mentioned in the editorial.Thanks!

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

    In my solution, I did a scan line, maintaining a partition of (currently) non-deleted rows into ranges in a map. There will be two types of events:

    1. Some deleted rectangle appears. In this case, we need to remove all the ranges that overlap with this rectangle's range (and output a new non-deleted rectangle) and create up to 2 new non-deleted ranges to the left and to the right of deleted range. For example,
    partition before:  [.1.][.2.]....[.3.]...[..4..][.5.]
    new deleted range: .......[................].........
    partition after:   [...][]..................[..][...]
    

    The algorithm will create non-deleted rectangles for ranges 2, 3 and 4 and create two new ranges as shown above.

    1. Some deleted rectangle disappears. In this case, we simply create a new non-deleted range. For example,
    partition before:  [...][]..................[..][...]
    old deleted range: .......[................].........
    partition after:   [...][][................][..][...]
    

    My solution for reference (submit).

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

Can E be solved for N ≤ 106? It's possible to calculate knapsack for this constrains.

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

    Yes, should be doable in O(n*(sqrt(n)+log^2(n))) with FFT convolutions.

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

      How ?

      • »
        »
        »
        »
        8 years ago, # ^ |
        Rev. 3   Vote: I like it +5 Vote: I do not like it

        Here is the simpler version in and small constant (/64 with bit-compression): Let's split the weights into two groups: and w < k.

        1. In the first group, there are at most values (because their sum doesn't exceed n).

        2. In the second group, there are at most k distinct values, and each group of equal weight w can be replaced with O(logn) equivalent (for knapsack) weights: if there are m weights equal to w, let's replace them with w, 2w, 4w, ..., , .

        This way, we can reduce the size problem to at most weights, which can be solved with regular knapsack dp (possibly with bit-compression)

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

          Let's replace w,w,w by 2w,w while we can (it my be done in O(n)), after that it will be only different weights by the same bound.
          So extra log is not needed

»
8 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Can someone give more detailed explanation of problem D ?

Thanks

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

    Ok, I'll try explain my solution

    Oleg can't go near offices he visited, so if now he is in office v, he visited q, q > v, he can't visit office k, where k > q. That means if now he's in office v, he can only visit office q, l < v < r, l is rightmost visited office which position is less than v and r is leftmost visited office which position is more than v. So we have to solve our problem for state [v][l][r][s], where s count of steps we made. We have 2 options. First is go to left and second is go to right. If we go left, we are not interested what is r, and if we go right we are not interested what is l. So our state now is [left border][right border][direction][s]. State [v][l][r][s] becomes 2: [v][r][right][s] and [l][v][left][s]. We can exactly identify our current position. If we go left it's right border, and if we go right it's left border. Now, when we are in state [l][r][direction][s] and now we are in office v, we have to just find all offices q, l<=q<=r and c[v][q] != 0 and solve our problem for [l][q][right][s+1] and [q][r][left][s+1] and take minimum. For it we can use recursion. We can start from any office, so we have to solve problem for [0][v][left][0] and [v][n+1][right][0] and take minimum.

    We have n^2*2*k states. In each state we check make not more than n operations. So algo works in O(n^3*k)

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

      How is the time complexity O((n+m)*n*k). I didn't even understand how the naive algorithm works in O((n+m)*n^2*k)

»
8 years ago, # |
  Vote: I like it +28 Vote: I do not like it

Problem G can be solved using Hopcroft-Karp algorithm, if you optimize it hard enough: 26669894.

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

I think problem B has a better solution with brute force and a little dp firstly, initialize 2 arrays h and v so that h[i,j]=number of cell with road work from (i,1) -> (i,j) and v[i,j]=number of cell with road work from (1,j)->(i,j) since igor can only make 2 turns so his way must be one of these: --- — ---- | ^ | | | | | | <-- --> | V ---- so we just need brute force and calculate the number of cell with road work on the path (get it from h and v cause he can only go up, down, left or right)

»
8 years ago, # |
  Vote: I like it +13 Vote: I do not like it

Problem F has a tag "divide and conquer". Is there a solution with O((n + m + q)polylog(n + m + q)) time complexity?

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

    Yes. Check submission of niyaznigmatul: 26621779.

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

      Thanks!

      For other people who are trying to understand this code: The setmin function sets every element of the interval to be the minimum of its value and val. The getmin function returns the first index in the interval whose value is larger than val.

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

I have a solution for problem F with time complexity O((N+M)logN) using segment tree.

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

    Would you be nice to explain your solution? thanks in advance

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Would you be nice to explain your solution? thanks in advance

»
7 years ago, # |
Rev. 3   Vote: I like it -16 Vote: I do not like it

Just in case someone is looking for a proof for problem A (793A - Oleg and shares)

We can't increase the values of any number, so we have to make bigger numbers equal to smaller numbers by subtracting k (zero or more times) of them.

Suppose the array is sorted.

At some point we'll have to make numbers arr[i] and arr[0] equal, and the only way is by subtracting k from arr[i] some number (q) of times and subtracting k from arr[0] some number (p) of times so we have: (a = arr[0], b = arr[i] for simplicity, 0 < i < n)

a — pk = b — qk

(b — a) = k (q — p)

We don't know (and we don't need to know) values p and q, but now it's certain that (b — a) must be divisible by k.

Now the key step is that if k divides (b — a), then we can make b equal to a by subtracting k (b — a) / k times because the only criteria to make a = b — qk is that k divides (b — a). This is optimal.

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

Getting W.A. for problem B......Can anyone provide a test case for which this fails?

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

Very easy solution brute force solution B- no DP or dfs/bfs at all. It takes full advantage of the fact that the solution must use at most 2 turns:

Because of the two turns limitation, there are two possible cases for our final path. Our directions for the travel can either be D/U => L/R => D/U or L/R => D/U => L/R.

Assuming the two locations are (r1, c1), (r2, c2), the first case case can be handled as such:

Iterate through all subarrays board[i][c1:c2]. If a subarray with 0 obstacles inside is found, and the subcolumns board[i:r1][c1] and board[i:r2][c2] also contain 0 obstacles, then we've found an answer. These subarrays/subcols by themselves will represent a valid 2 turn path, so if they contain no obstacles, it is obvious an answer has been found.

This same logic can also be applied to the second case. If searching both cases yields no answer, then we can safely return "NO".

Accepted implementation: https://codeforces.me/contest/793/submission/164203477