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

Автор zoomswk, 9 лет назад, По-английски
Tutorial is loading...

Official Implementation: http://ideone.com/jsXSst

===

Tutorial is loading...

Official Implementation: http://ideone.com/YD7s4S

NOTE: If you get TLE, use faster input methods. For example, use scanf instead of cin.

===

Tutorial is loading...

Official Implementation: http://ideone.com/B9pjyi

NOTE: If you get TLE, use faster input methods. For example, use scanf instead of cin.

===

Tutorial is loading...

Official Implementation: http://ideone.com/nlPVW0

===

Tutorial is loading...

Official Implementation: http://ideone.com/cwP55b

===

Tutorial is loading...

Official Implementation: http://ideone.com/FfnJyp

===

Feel free to ask any questions below. It's hard to write an editorial that satisfies the need of everyone. I hope you'll enjoy solving the problems :)

  • Проголосовать: нравится
  • +79
  • Проголосовать: не нравится

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

Suppose this is not optimal, and k’ + c (c ≤ 0) roads can be shut down. It should be c >= 0

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

In C, each bank’s strength can be increased at most twice

Isn't it, each bank's strength can be increased at most [ sum of adjacent node's degrees ] times. eg: consider the star graph. However the overall total updates will not exceed 2 * ( n — 1 ).

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

Problem E can be solved in as well using the dp which was used in IOI 2016's Task Aliens. 26284310

UPD as mentioned below 26298863.

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

    Also you can instead of storing benefit of both geniuses assume that one of their benefit ended in that prefix, so original solution will be O(N * P) or with lambda optimization O(N * log(P) * K). It's awesome!

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

      Can you explain the lambda optimization or give a link where it is described? Is that one that is described in blog named "Incredibly beautiful dp optimization from N^3 to N^2logN"?

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

        yes , also check out editorial for ioi 2016's last problem , that has a pretty good explanation of this technique.

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

          I have two questions about your code, thanks in advance if you can help clarify my doubts:

          1. Is the mult constant used just for the purpose of rounding the binary search?
          1. I am not exactly sure how does the penalty parameter in the check function helps to monitor the amount of peeking — is it used for setting a baseline for the score gained in each peek assuming that none of the peeking overlaps?

          Thanks again. :)

          • »
            »
            »
            »
            »
            »
            9 лет назад, скрыть # ^ |
             
            Проголосовать: нравится +5 Проголосовать: не нравится
            1. mult is used to avoid doubles.
            2. if you increase penalty , the number of items picked will be less and if you decrease it , the number of items will increase , so you can binary search on penalty so that exactly K items will be used.
»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Would you mind elaborating what a and b in dp[i][j][a][b] in problem E are? I don't get what you mean by this benefit.

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

Where are the official solutions ?

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

My solution in C that considers two starting vertices : any vertex with maximal value and vertex that has the most neighbors with maximal value passes the tests, can anyone prove correctness of such solution?

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

Can someone please elaborate more on C?

I didn't get the part after each node's strength is incremented by 2.

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

Official Implementations are not available!

It says "You are not allowed."

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

C alternate solution:

  • if the MAX number appears only once, need to run a bfs (for case like: 8->7->7 (ans:9))
  • else if we can find a node where all MAX numbers are within its 1 level, ans is MAX+1
  • else ans is MAX+2

code

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

can someone please explains: Suppose this is not optimal, and k’ + c (c ≤ 0) roads can be shut down. The tree will break into k’ + c + 1 components in problem D?

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

i used problem D with dfs...And got wa on 6....I don't know why? here is my code :26286839 please help me....

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

    Last night,i also used dfs and got wrong answer. I found that my Dfs solution will give wrong answer on this test case.

    Image of the test case

    In my dfs visit order, i first vist the right police node and will not visit "visited-node" again. So when i start at the left police node, i can't visit the X-Blue node ==> wrong answer.

    That is my case. Hope it help :D

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

      I used dfs with cities containing police stations as starting vertexes while keeping track of distance of the current node from current police station during dfs. I removed a road if that road connects to the node whose distance is greater than d from the current police station. I also removed the road if that road connects with other police station because if from a city we can reach to more than one police stations in different cities with distance less or equal to d then there is no need for the extra road that leads to more police stations from that city.

      But still getting the wrong answer on test #6. Any help would be appreciated....

      My Submission

      Thnx...

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

For Problem D

"With this method, you can see that exactly k’ - 1 roads will be shut down"

Can anyone explain this statement

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

    Imagine all cities being associated with their closest police station, and collapse this cluster of police station with it's associated cities into one node. This is always possible as the problem states that each city can go to at least one police station with distance <= d.

    Now, we have k nodes in the graph each representing one cluster. Remember that originally, we had a tree. So even after collapsing, these k nodes are still connected to each other and form a tree.

    Now we know that there exists exactly one path joining each of these k nodes, and we just have to delete these paths, as there is no need to join two clusters, since every city is already associated with one police station. And, for k nodes in a tree, we have exactly k-1 edges.

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

Why Problem C can be solved within O(N)? cannot understand the standard solution's algorithm.. Someone can help me?

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

"Why? It is because each bank’s strength can be increased at most twice, once by a neighboring bank, and once by a semi-neighboring bank".I don't understand why only once by a neighbor or semi neighbor since a node can have multiple neighbors .... can someone kindly explain what i am missing ? Thanks .

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

Is problem C solvable using DFS?

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

One of my friends had a small problem while solving the C on the last contest. He used cin.sync_with_stdio(false); and cout.sync_with_stdio(false); and he got runtime error. We used this things and had no problem but this time got runtime error.. Is it a special rule for this ? Or where does this problem come from?

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

Where did editorial for problem C disappear? :)

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

DIV2-C: Got TLE in testcase 35. can anyone help me in analysing the time complexity of this submission Thank you in advance :)

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

Sorry! I don't understand why "y" is incremented here if(a[pos] == maxval) x--, y++;

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

    Because they are neighboring, so its value is to be decreased by 1. From maxval, it would become maxval - 1. x should be decreased and y should be increased.

    On the other hand, if a[pos] = maxval - 1 you do not add anything, because it becomes maxval - 2 but we keep track of maxval and maxval - 1 only.

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

What is a counterexample to the greedy solution of D? I don't understand why this doesn't work.

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

I have something wrong with problem D. My two submission (1 in-contest, 1 practice, but both are the same) got TLE on different test cases, although it provided the output.

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

Problem C can be solved with the segment tree. It's really faster than multiset and data structures like it. Maybe it a bit harder in implementation, but we get so far from TLE instead of multiset. Maybe, someone can suggest more easier way? ( I know about O(N) solution, but interesting in O(NlogN) with data structures ). Solution with segment tree ( 700 ms ) — http://codeforces.me/contest/796/submission/26302092 Solution with multiset ( 1800 ms ) — http://codeforces.me/contest/796/submission/26299257

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

I used greedy approach for problem E. I got WA on #12, the input is big, so I can't check by hand, where my algorithm went wrong. Probably there is a mistake in my theorem about the algorithm's correctness. Can someone point out why is the approach wrong, or tell, I have only failed in implementation?

Algorithm: We store 3 boolean arrays, the answers first and second genius got right, and the answers we have already copied. We check the maximum copiable answer number (we can do this in O(n) by pushing a window of size k from 1-n), then we copy the answers which produces the maximum copiable answers. We do this p times, so every peek we get the maximum number of answers correct. Total runtime is O(np).

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

What is so special about testcase 13 in problem D? My code seems to do the same thing as you wrote :/ http://codeforces.me/contest/796/submission/26274912

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

Can someone explain state transitions for problem E in detail?

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

Problem C:

What is the differance b/w map and hashmap?

Also, from editorial "However, each operation involves the map data structure, so the overall runtime is O(nlogn)" How map access adds log(n)? Isn't map access is a constant time operation O(1)?

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

    C++ map and Java TreeMap are usually implemented with a balanced tree (like Red-Black) with each operation being O(log n) while C++ unordered_map and Java HashMap use a hash table with amortized O(1) operations.

    Thus n operations on a map/TreeMap take O(n log n) time but only O(n) amortized for unordered_map/HashMap.

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

Waw problem E solution is pretty dense

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

Hello, for problem D your algo gives wrong output for following input(at least my implementation did :( )

5 2 2
1 4
1 2
2 3
3 4
3 5

the output should be 1 2. But mine give 1 3. (thus leaving node 5 3 miles away!)

The problem is, If we traverse those cities with police stations as given in input city 3 is marked visited by city 1. And when city 4 comes to city 3 it finds it already visited and cut the road(This will leave city 5). How can i fix it?

If someone is interested in source code it can be found 26301147

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

Thanks for the useful editorial.

In the Official Implementation of Problem C,

the // plus part (lines 44-51) could have been replaced with two lines:

In line (28): int x1 = x, y1 = y;

In line (43): x = x1, y = y1;

The following verifies the correctness of the observation:

http://codeforces.me/contest/796/submission/26337076

And the following is a C++ class implementation:

http://codeforces.me/contest/796/submission/26321000

Best Regards

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

So at first I didn't really understand why we need to clear out DP[next_layer][j][a][b] to a negative instead of just 0's at the end of each i iteration. Is this because DP[i][j][a][b] is not really "valid" for the case where i + a < k or i + b < k when j >= 1?

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

In official implement of problem D.

can someone please explain why set v[pos] = 1 here

   if(v[pos]) continue;
        v[pos] = 1;

instead of set v[way[pos][i].first] = 1 when

   else q.push({way[pos][i].first, pos});

is met?

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

    It's a matter of style. I'm just used to implementing this version of BFS. :)

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

      But I get WA if I modify your code

       while(!q.empty()){
              int pos = q.front().first;
              int from = q.front().second;
              q.pop();
              // if(v[pos]) continue;
              // v[pos] = 1;
              for(int i=0; i<way[pos].size(); i++) if(way[pos][i].first != from){
                  if(v[way[pos][i].first]) res[way[pos][i].second] = 1;
                  else{
                  	q.push({way[pos][i].first, pos});
                  	v[way[pos][i].first] = true;
                  } 
              }
          }
      
      • »
        »
        »
        »
        9 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Sorry for the late reply. It shutdowns all roads connected to cities with multiple police stations. You have to modify the algorithm to push to the queue only once per each city with police stations.

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

Problem E In your code, you didn't calculate dp[i][j][k-1][k-1].

dp[i][j][k-1][k-1] = max(dp[i][j][k-1][k-1], dp[i - 1][j - 2][0][0] + (A[i] | B[i]));

Why is it right?

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

Could you tell something about how to find the definition of the state in DP in problem E? I find it rather difficult.

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

can someone explain problem E clearly ??

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

I am having some trouble trying to upsolve D. I don't know if it is my approach that is flawed or the code. My idea is start at a station then DFS to of a distance of 2*d+1. If I find a station while doing this I immediately backtrack and make a cut halfway between the two stations if there are an even number of nodes between them or closer to the original station if there are an odd number of nodes between them. Then I store the cut and stop exploring the part of the branch below the cut. I repeat until I either make k-1 cuts or have started a DFS from each station. My code http://codeforces.me/contest/796/submission/26412721 works until case 7 which is so big I can't figure out exactly where it goes wrong. If anyone could point out an error in the approach, code or generate a smaller test case that breaks it that would be appreciated.

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

In problom D. example 5; There are 300000 plice stations,but the answer is not 300000-1. I don't know why.Could you help me?

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

in problem c, If there are 5 nodes,with strength 10,1,1,1,10 and edges are (1,2),(2,3),(3,4), (4,5). If we hack in sequence 1,5,2,3,4 The answer for the above case should be 10 but your code is giving 12.Please why it is wrong

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

Couple things with the solution for Problem C that I am confused about.

  1. What exactly do x = 0 and y = 0 signify? Figured Out: The ties between all the (maxValue)s and (maxValue-1)s are severed.
  2. What is the point of going back through and setting x and y to their original values before a particular iteration of the "i" loop? Wouldn't a dummy variable save time?
  3. Why are semi-neighbors not checked in the "j" loop? Figured Out: You already know the answer is one of three values, and you just need to check if all the (maxValue)s and (maxValue-1)s are in the same "flower" (one center node that is (maxValue) with "petals/leaves" stemming out of it to (maxValue-1)s).

Can someone now verify my arguments?

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

    2. I didn't think of it. You can create dummy variables if you want.

    3. We assume that hacking each bank i needs strength ai + 2, but this is not true for the bank you start with and its neighboring banks, so we have to update the strengths required to hack those banks required accordingly. Hacking semi-neighbors cost ai + 2 anyway so we don't have to update them.

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

In the solution for Problem E, is the switch between curr and prev a result of which 3-D table has the previous values? As in, do the previous values alternate between being stored in i = 0 and i = 1?

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

for problem D: the variable d is unuseful?

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

Can anyone help me for the solution of E pls? I think it should have F[i][j] which represents for maximum correct answers she gets in first i questions with j times glacing. I know it's wrong, but I can't find why it's wrong.

This is my code. I have pre[i][j][3] array for pre-calculating in segment l-r, how much answers she can get (pre[i][j][0] for both copy from 2 geniuses, pre[i][j][1] for the only the first one, pre[i][j][2] for only the second one).

Pls help me!! Thanks so much

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

I solved E with O(n^2). At first my approach was O(npk) (not O(np(k^2)) ) but after I saw the nice trick used in the editorial to improve the order I reached O(n^2) approach. any way this is my code

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

Hi for problem F ("Sequence Recovery") , I have an counter example for my code(and also it's for tutorial code) but it got AC! this is my code : https://codeforces.me/contest/796/submission/73989810 this is counter example:

2 3

1 2 2 6

2 2 2

1 1 2 2

it should be : YES 1 6

but it return : YES 2 6