Блог пользователя Livace

Автор Livace, история, 8 лет назад, По-русски

Огромное спасибо всем, кто участвовал! Это правда важно для меня. Если после разбора у вас остались какие-то вопросы, не стесняйтесь писать мне на codeforces/в вк/gmail.

877A - Леша и сломаный контест

tutorial

877B - Никита и строка

tutorial

877C - Слава и танки

tutorial

877D - Оля и энергетики

tutorial

877E - Данил и подработка

tutorial

877F - Аня и книги

tutorial
Разбор задач Codeforces Round 442 (Div. 2)
  • Проголосовать: нравится
  • +75
  • Проголосовать: не нравится

»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +4 Проголосовать: не нравится

What is test 49 in D(it is long hence not visible)? NOTE: Ignore got it.

»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +17 Проголосовать: не нравится

How is BFS too slow for D? 31650245 is BFS, but it doesn't time out.

»
8 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +15 Проголосовать: не нравится
»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If someone would be kind enough to tell me what went wrong with my E submission (http://mirror.codeforces.com/contest/877/submission/31648193). Thanks

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Can you provide a java solution for problem F that passes the time limit? I think this submission is the same as described in the editorial and it gets TLE.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

Problem D

Test case 31?

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится
    5 5 3
    ..#..
    .....
    .....
    .....
    .....
    3 3 1 2
    

    invalid function also checks for visited points, but there might be some unvisited reachable points which are farther than first visited point. Do not think that reordering of direction array will help :)

  • »
    »
    8 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +9 Проголосовать: не нравится
    7 14 14
    XXXXXXXX..XXXX
    XXXXXXX...XXXX
    XXXXXX..X.XXXX
    XXXXX.....XXXX
    XXXX..X...XXXX
    XXXXXXXXX.XXXX
    XXXXXXXXX.XXXX
    1 9 7 10
    

    the visit order may be the cause. So you need to memorize the direction too, seen[f][c][d] and that should fix it.

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Can you please explain why do we need to save direction, too?

      • »
        »
        »
        »
        8 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Well, one of the reasons is because you can calculate the cost in order and be sure is good calculated. What I mean is, if you keep moving in the same direction, you go with cost 0, but if you change the direction, your cost increases in one. (Now you can use bfs 0-1). Now, note that if you don't do this (only save row and column), it can be possible that you reach a cell with a cost greater than other, and because you reached that cell first (because of bfs) with greater cost, you assume that is the best way to reach that cell, but that might not be the best option.

        So basically you need to separate the two kinds of costs, 0 for same direction, 1 for other direction.

        I'm thinking maybe if you use a heap instead of queue to process in order, the dimension of the directions may be not needed (Is a thought. You can test it. Because basically the bfs 0-1 is a dijsktra but with special costs.)

        Is it clear?

        • »
          »
          »
          »
          »
          8 лет назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Thank you a lot for such a great response & I'm really sorry for my so long one.

          Everything is pretty clear, but I still didn't get the idea why I should save direction.

          Because let's assume that we stay in state (x, y), and we'll just brute force some count in range[1; k] and anyway we'll do +1 to our adjacents to which we can go in one step.

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks Temirulan and -osdajigu-. Got AC

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Any implementation as mentioned in Editorial..... Thanks.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Does anyone have any idea why this- http://mirror.codeforces.com/contest/877/submission/31647784 gave WA on test 49?

»
8 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится +5 Проголосовать: не нравится

LOL, For 5 seconds I was waiting to load the tutorial of problem C. :P

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

Proof of optimality for Problem C ?

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +24 Проголосовать: не нравится

    The problem is equivalent to finding a string S such that for all 1 < i ≤ n, i - 1 precedes i somewhere in the string and i precedes i - 1 somewhere in the string.

    Now observe that for some i, it occurs either 1 or 2 times in the optimal string. If it is more than 2 times, we can erase the middle ones and obtain a better string.

    If for some i, the number of times is 1, then i + 1 must occur (at least) 2 times: to the left of this location and to the right. If i occurs 2 times, then i + 1 must occur (at least) once (in between the two occurences).

    From this, you can build the string 2,4,6,... 1,3,5,..., 2,4,6, ... which is therefore optimal.

    At every step we get some partial string with all the numbers from 1 to i. Inductively you can show that this is optimal at every transition to i + 1.

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Can you please explain, how did you reduced the given problem to equivalent string problem?

      • »
        »
        »
        »
        8 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Suppose there are 2 tanks at every location i. Suppose on first hit, the first tank moves to the left, the other moves to the right. This is the worst case of tank positioning, so if this case is handled by the string, it is handled by every configuration of tanks.

        To erase the first tank from location i, we have to bomb location i. After this, the first tank moves to location i - 1 so this one has to be bombed after i is bombed. Likewise, to bomb the second tank, we have to bomb location i at some point, and after that we still have to bomb i + 1. You can substitute i with i - 1 to obtain that i - 1 has to precede i at some point in the string.

        The optimal bombing will fix this situation I described here, so it will obey the cosntraints on the string I described. On the other hand, if the constraints are met for a string S, then a bombing plan will be a 'good' bombing plan.

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    It is easy to notice that we have to hit atleast 3 times in each pair of neigbor cells.

    That is why answer is atleast 3*n/2 (+1 if n is odd).

»
8 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone provide the recurrence formula for B with DP?

Never mind, figured it out!

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

А можно нормальное доказательство C ?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem C what if i throw bombs on all cells starting from last to first and then again in the 2nd cell.It gives n+1 as my answer.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

can someone please provide the code for D

»
8 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится +1 Проголосовать: не нравится

I am totally missing the point of how the sets are used.

Now it's easy to find all not visited cell which is reachable from vertex

I don't get it. Isn't the following also easy? (PSEUDOCODE)

int r_cur = queue.top().row;
int c_cur = queue.top().column;
queue.pop();

int dr[4] = {1, -1, 0, 0};
int dc[4] = {0, 0, 1, -1};
for (int dir = 0; dir < 4; dir++)
{
    for (int len = 1; ; len++)
    {
        int r = r_cur + len * dr[dir];
        int c = c_cur + len * dc[dir];

        if (!empty[r][c] || !valid(r, c))
            break;

        if (dist[r][c] > cur_dist + len)
        {
            dist[r][c] = cur_dist + len;
            queue.push({r, c});
        }
    }
}

It is easy to find all unvisited cells from the current cell (ri, ci) in O(n). Right?
It looks like I fundamentally don't understand what we are looking for and why do we need sets for that.
Ultimately we need to fill the matrix dist[N][M] and we can reach every single one of dist[r][c] from adjacent cell in O(1).

I know that dist[r1][c1] = 0 and all of the vertical cells dist[r1 ± i][c1] and horizontal cells dist[r1][c1 ± i] have distance 1 (unless they aren't too far from (r1, c1)). This is the point zero in my current understanding. Could you please, elaborate what do we do starting from that understanding?

  • »
    »
    8 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +1 Проголосовать: не нравится

    First, simple, but slow BFS solution: just connect each cell to the cells with distance no more, than k, in each direction and simply run BFS on this graph. This will run in O(nmk), because for every n·m node we will check O(k) adjacent nodes.

    So for each row and column, let's put all nodes, that are not already reached by our bfs process, into set. Then, when we take node from queue, we look to the nearest not reached neighbour in each direction (can be done with log(set size) via set::lower_bound) and if the distance is no more, than k, extract it from its both sets, put into queue and continue looking for the new nearest neighbour. So in each step we look at 4 + number of extracted in this step nodes. And total sum of extracted nodes is no more, than total nodes count (after we extract node, we will never look at him again).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +43 Проголосовать: не нравится

Hi! As a tester, I enjoyed solving the problems. Thanks to the author (Livace), the contest was really good in my thought.

It could be better to swap D and E. Anyway, E and F were technical problems (i.e. E needed fenwick Or segment tree, F needed MO), D was creative, and maybe hard to code, A, B and C were straight-forward.

To summarize, I think it was the best contest I've tested if I remember correctly.

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Can you please explain how to flip 1 and 0 using fenwick and then get the count of 1? i solved it using segment tree but would like to know solution using fenwick trees(i know how to do range updates in fenwick like if i wish to add a particular value to a range l to r but cant get any idea of how to flip)

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +5 Проголосовать: не нравится

    Hi! In D BFS with some breaks passed in 77 ms. Proof. Was this intended? Because I can understand the solution from editorial and why it works, but face some problems trying to estimate complexity of such bfs.

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +12 Проголосовать: не нравится

      This condition if(ans[ni][nj] < ans[v.fi][v.se] + 1) break; gives you O(nm) complexity. Because consider cell's ans is x, than throught entire algorithm's work you will visit this cell no more than 4 times: one for each of 4 directions from the nearest cell in this direction with ans = x - 1.

»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +9 Проголосовать: не нравится
My idea for D

UPDATE: My mistake. Seems like I didn't read the problem statement carefully enough.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

for problem number C. let's assume n = 6. now throw bombs in all even positions now all tanks are in 1 3 and 5 positions. now throw bombs in 1, 3, 5 positions. now the tanks are in 2 and 4 positions, isn't it? cause it's said that tanks in the cell n can be moved only to n-1 position and tanks in cell 1 can be moved only to 2 cell.now throw bombs in 2 and 4 positions. please tell me why I throw a bomb in position 6 again? why I waste a bomb? why is the answer not 8?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Could someone please explain how B can be solved in Linear time? I get Time Limit exceeded for the editorial solution which is O(n^2)

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How could people implement E in 10-15 minutes during the contest? I used segment tree with lazy propagation, and it took like 1 hour to code, and another 1 to debug. The latter could be avoided, but even if I coded faster, 10-15 minutes seems impossible.

I checked fast solvers submissions, but they used one character variable names, and unusual spacing, so I couldn't really understand their solution.

Could someone explain a faster to implement solution? Or did they have pre-implemented lazy prop?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Можете объяснить, что такое Эйлеров обход?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why i keep getting memory limit exceeded on problem D? is it because too many queue?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

31659292 877B - Никита и строка Hey can somebody help me find out what is wrong with my solution to B — Nikita and String. I got wrong answer in test case 37. Thanks.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Any implementation of problem D as mentioned in Editorial. Thanks...!

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in F how left border can be moved easily??

let current left is l, updating l to l + 1 removes a[l] from all prefixes so all cnt values should be updated which should be O(r — l).

how is this done efficiently?? or i'm missing smthng.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why is my code giving RE for test 5 in [problem:D].

»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I think that E can be solved using sqrt decomposition but I can't do it.

Can someone help me please each time I submit this 31659189 code I get RE on a different test. I don't know what's wrong and the code is working on my mechanic. When I get RE on some test I use the test in costume test and it works. It's really weird.

I found what's wrong now I'm getting WA on 14 if someone could help I would really appreciate it. here's my new submission 31687428.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I used unorder_map instead of map for F,but I still got TLE.I want to know unorder_map is very slow on codeforces?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

why did my solution get TLE http://mirror.codeforces.com/contest/877/submission/31697709 for F? \n used unordered_map

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem E,Can someone guide me on how to form segment tree from given tree ?

»
8 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

BFS why i have Memory limit exceeded on test 5 ? 31660164

and why i have AC if i use std::set<std::pair<int, std::pair<int, int> > > ? 31660401

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

First looking at the solution for D and E, I think it is very unclear. But then after thinking for a while, i got the answer.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

We can solve F by using Mo's algorithm if we discretize the numbers first..

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Could someone tell what test case number 46 for D is? :| I've been thinking on it for a long time with no avail :/ http://mirror.codeforces.com/contest/877/submission/31757764

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Regarding Problem D solutions
which do not use ordered sets mentioned in the tutorial.

I tried to understand why we can not stop early (after we encountered a cell which had been assigned a non-infinity value already).
Finally, I found this test case useful:

s.#  
..d

where s and d are the source (x1, y1) and the destination (x2, y2) correspondingly, K > 1.

I suggest that this test case (in its eight versions) is a good way to quickly check an early stopping solution for correctness.

The eight versions of the test.

For those interested

there are more details.
»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Hi, I know this is an old contest but I've been trying to do problem E for days now and I would really appreciate some help. I am getting TLE on test case 13 (Code takes over 30 sec). However, I can't tell what is wrong with my complexity. I'm really stuck ): http://mirror.codeforces.com/contest/877/submission/33872973

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain how to solve F using flows?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anybody please tell me what is wrong with my DP solution for div2 B? I am getting WA on case 15. Here is the code. 39735958

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why isn't the answer for C, n+1, i.e, if you drop a bomb from the nth cell then on the n-1,n-2, — — — ,2,1,2. Won't that destroy all the tanks? I can't see why I am wrong. Any help appreciated.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

.

»
5 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In problem D, instead of using sets to find the nearest unvisited neighbor in each direction, we can also use DSU (Disjoint Set Union) for each row/column in which every cell would point to the next unvisited cell, you can follow this link for more details about how DSU can be used for this purpose.

Submission for reference: 101039018

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

problem F: can someone explain me how to solve this problem in O(n.q.log(n))? i don't understand the solve above

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Use map for F will result TLE lol, you should compress the prefix and pre calculate the next and previous of a(i). That's way better.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

About Problem D:

My submission 234897503 is Accepted but I was wrong in this test:

3 5 1000
....#
.##.#
.....
1 1 3 5

The correct answer is 2 but my output is 3. Can you add this test to this problem