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

Автор cyand1317, история, 9 лет назад, По-английски

Greetings!

Codeforces Round 418 (Div. 2) has just come to an end. This is my first round here, and hope you all enjoyed the contest! > <

Seems the statements still created a few issues :( Anyway hope you liked it, and the characters feature the Monogatari anime series. Let me state again that the statements has little to do with the actual plot — they're inspired by five theme songs actually — and I'm not spoiling anyone of the series! ^ ^

Sympathy for those failing system tests on B... This problem was intended to experience a lot of hacks but somehow there are not so many.

Here are the detailed solutions to the problems. Feel free to write in the comments if there's anything incorrect or unclear.

Tutorial is loading...
Solution 1
Solution 2
Tutorial is loading...
Solution 1
Solution 2 - Casework
Tutorial is loading...
Solution
Tutorial is loading...
Solution 1 - DP on trees
Solution 2 - Greedy

One more thing — Staying up late is bad for health.

Tutorial is loading...
Solution 1 - O(n^5)
Solution 2 - O(n^3) by KAN

This is the round with the most solutions so far perhaps? There are at least 3 × 2 × 3 × 3 × 3 = 162 different ways to pass all problems in this round =D

Pla-tinum happy though I'm supposed to be
Pla-tinum sad is somehow how I get

Personally I'd like to express my gratitude to the community for creating this amazing first-time experience for me. Thank you, and see you next round. Probably it will be for Ha... Well, let's wait and see :)

Cheers \(^ ^)/

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

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

Thanks for fast editorial :)

Please fast rating update too :)

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

Bonus. Figure out why solution 2 is not hackable.
Because all permutations of b (maybe except sorted) is valid?

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

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

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

Thank you for the great problems, especially problem C and D :)

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

Problem E is awesome. I had a O(n^6) solution at the beginning and made it O(n^5) soon. Then I spent nearly an hour to optimize it. Finally I got a O(n^4) solution and the contest ended.

Anyway this problem is a really graceful dynamic programming problem in my opinion, which made me doubt whether I was solving problem in topcoder. Just an analogy, don't mind :)

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

why is my solution got TLE ?

cyand1317

27649598

same complexity passed with many others

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

anyone who completed problem c in java ?

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

Can anyone please explain the solution for Problem — C.I am not able to understand the editorial.

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

    Which particular part of it? Perhaps I can make it clearer.

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

    Clearly, given a color c and a number of repaints m you could bash through all possible intervals and find the maximum valid substring, but this would take too long, with n(n+1)/2 possible intervals and q queries, taking O(n^2*q) time.

    We can resolve this issue as there are 26n possible queries (as there are 26 letters in the alphabet and m is constrained to [1,n]. Therefore we run through every possible query and store its answer in an array Ans[c][m]. (this takes O(n^2*26) time)

    To run through every possible query we look at every possible color and every single possible subinterval, and find the minimum number of recolors needed to make that interval valid. We compare the size of this interval to the size currently stored in Ans[c][r-l+1-t], and store the maximum of the two.

    However, this only finds the maximum value when exactly m colors are recolored, but in some cases not all m colors need to be recolored, for example take string axaxa for favorite color a, a maximum of 2 recolors will be needed even if we have more than 2 recolors available. Therefore, for each color we iterate through [1,m] and if Ans[c][j] is less than Ans[c][j-1], we replace Ans[c][j] with Ans[c][j-1]. Using the above example, Ans[a][2] would = 5 while Ans[a][3] would = 0, and so we set Ans[a][3] to 5. (Restating what I said above: the maximum vaild substring given a number of recolors m is equal to the maximum valid substring that can be obtained by any number p such that 1<=p<=m. We originally only found answers which used exactly m recolors.)

    Then we handle each query by using this 2-D array as the solutions are precomputed.

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

Problem C. Got TLE with O(nq) solution. Any idea how to fix it? http://mirror.codeforces.com/contest/814/submission/27651582

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

    Try adding the following code lines:

    int main( ) { 
      ios_base::sync_with_stdio( 0 );
      cin.tie( 0 );
    
      // Your code here
    
    }
    

    I think that you got TLE because the input file is too large and cin/cout is a little bit slow without the lines above.

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

Bonus for C in O(nq) can be easily done using two pointers (sliding window).

Spoiler

Check my submission for better understanding. Code

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

    I have the same solution and interesting in if there is a guarantee about working less than 2 seconds ? I was hoping only on power of computers of the verification system. As I know, approximate number of actions per second about 100 millions. So, we have O(n * q) ~ 300 millions actions and only 2 seconds.

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

      Though the solution with complexity O(n * q) passes, you can still optimize that solution. As m <  = 1500, the total number of unique queries can be 26 * 1500(as there can be 26 different alphabets for each m). Hence the queries in the input will repeat. So you can save the answers for the queries as you encounter them, and if that query is repeated again, you can simply print it directly without solving again. You can do this by maintaining an array dp[27][1510]. Here are my submissions with and without this optimization.

      Submission 1: 27657218

      Submission 2: 27657190

      There is a huge difference in time.

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

    I did C using Binary Search on the answer and checking whether for the particular value of answer is it possible to paint a subsegment by combining it with the sliding window. Code

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

Can't understand the description of E. Must the graph be a tree?

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

Could someone explain me a solution of problem D, please.

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

    I solved D with Dynamic programming(memoization): D[1111][2][2][2]: D[node][|set1|_is_odd?][|set2|_is_odd?][node_belogs_to_set2?]

    Then the core part becomes

    D[node][odd1][odd2][at_odd2] = (odd1 && !at_odd2) || (odd2 && at_odd2) ? area[node]: -area[node] + get_optimal_combination(direct_children[node])

    Here, direct_children consists of literally indices of direct children, which means, for each y in direct_children[node], there is no other circle smaller than node which is parent of y.

    And get_optimal_combination is simple, too: this is just a sum of max(D[child][!odd1][odd2][false], D[child][odd1][!odd2][true]); (this is obvious so that I will omit the proof).

    Code here: http://mirror.codeforces.com/contest/814/submission/27661135

    P.S. I think this problem can solved using plain DFS without DP.

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

    If we define the level of a circle as the number of circles that completely enclose it, then for any circle, its effective area can be seen as sum of all the even levels minus the sum of all the odd levels. My solution was to move to the second plane all circles whose level is 1, that is, all the circles that have exactly one circle enclosing them. It's pretty easy to see why this works, but let me know if you need a proof. 27655767

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

In test case 6 of problem B:
10

1 2 3 4 5 6 7 8 4 10

1 2 3 4 5 6 7 6 9 10


my output is :

1 2 3 4 5 6 7 6 4 10

Jury's answer was :

1 2 3 4 5 6 7 8 9 10

But the problem says,it satisfies any permutations maintaing (n-1) matches..I can't find any errors with my output.It would be very helpful if someone helps me with my confusion.Thanks in advance.

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

I can't understand how spaciousness is defined in problem D?Is it unique for given set of circles.How are those odd ranges chosen?P

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

    The spaciousness is the total area that is covered by an odd number of dancers. So, for example, if a region is covered by only one dancer, this region is included in the computation of total spaciousness, if a region is covered by exactly two dancers, it is not.

    Your goal in the problem is to maximize the total spaciousness by allocating some dancers to dance only after midnight and some to dance only before midnight.

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

c problem .....in the editorial code->what is the use of this loop:-

for (int i = 1; i < MAXN; ++i)
            ans[c][i] = std::max(ans[c][i], ans[c][i &mdash; 1]);
»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand what is this "26" in the editorial of problem C

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

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

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

Can anybody explain how the tree structure in problem D can be built in O(n log n) ?

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

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

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

Ah ha~~~ What do you mean by using 'Let's wait and see." ~ ^_^

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

Is problem C a form of a classic DP problem??

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

I couldn't understand DP on trees for problem D.Can someone explain a bit more on that DP part?

Thanks in advance and sorry if the question seemed to be trivial.

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

how could u construct the tree for D in ?

particularly plz specific how to deal with two circles tangent with each other.

coz i used sweepline + set which leads to Wrong answer on 4 >.<

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

I don't understand the formular f[u][0/1][0/1] of D's solution. What does it mean?

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

    Let F[u][isOddGroup1][isOddGroup2] be the best sum for distributing the circles between first group and second group optimally, so you can either make the circle u in the first group or the second group, and either sum or subtract the circle u area according to the parameters isOddGroup1 and isOddGroup2 respectively.

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

    f[u][p1][p2] is the maximum total answer in the subtree of u, assuming there are p1 nodes assigned to the first group and p2 nodes assigned to the second group among u's ancestors. And we only need to care about the oddness/evenness of p1 and p2 so we store them modulo 2.

    Edit: Seems I was a bit too slow XD

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

      For a node u, we look at its ancestor, that's clear. What's not clear is why we use u's subtree to calculate f instead of its ancestors. The provided solution does exactly that, doesn't it?

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

        In the editorial, p1 and p2 are kind of assumed. So f means "what's the best we can get in the subtree, if the ancestors are in this state." We calculate this value bottom-up, and u's value will be used by its parent.

        Other approaches may not use ideas like this, though.

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

Can any one guide me how to do Problem c with top down dp approach.I have written the recursive implementation but i am unable to memoize ,that's why i am getting tle.Thanks in Advance.

Here is my solution link

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

In problem E, why can't we club p1 and p2, and club c1 and c2, and have dp[u][p1 + 2*p2][c1 + 2*c2], which denotes number of ways to build the graph with the first u vertices, with (p1 + 2*p2) plugs in the previous level, and (c1 + 2*c2) in the current one?

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

why title of C and E is not English?

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

Hello,

what do we mean by Figure out why solution 2 is not hackable.

what is the meaning of hackable and why is not hackable.

is the solution 1 hackable?

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

In problem D I can't understand this statement " consider a vertex u, and the parity (oddness/evenness) of number of nodes in its ancestors from the first and the second group."

in this matrix f[u][0/1][0/1] what represent the second and the third dimension I didn't get it

thank you in advance

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

You can solve D with pure DFS, no DP needed.

for every tree, Add the root to the first group, and the the children of the root to the second group , so answer = Area of Root + Area of each of the root's children, after that either add the area of the current circle to the total answer if the depth is even , or subtract the area if the depth is odd.

This works because, if The maximum depth of the tree is 2, it is optimal to put the root on one side and the root's children on the other side,then you simply add all the areas to the final answer,but if you add another smaller circle into the plane, you increase the depth by one so this new circle area will definitely get subtracted if put on any side,we solve this by adding its children(which have depth4) inside it(to decrease the loss of area),then same goes for circle of depth 5 and its children.

http://mirror.codeforces.com/contest/814/submission/27689113

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

Can you explain for me how to dp array WAYS of solution n ^ 3 in problems E.

I don't understand how it work and remarks in code such as // 2-0, 2-0 // 2-2, 2-0 .... etc !

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

    Just a rough idea for understanding the sample solution: consider what happens when we add edges among vertices of the same level (with c1 "1-plug" vertices and c2 "2-plug" ones), and from a vertex in that level to a vertex in a new level (with c0 vertices). "Plugs" of certain vertices will decrease (some "2-plug" vertices become "1-plug" for example), depending on the edges we choose.

    We take a vertex and consider how they're connected to other vertices. The comments are in the form of "(the vertex we're currently considering) — (the vertex it's connected to)". e.g. "2-1, 2-0" means we take a "2-plug" vertex and connect it to a "1-plug" vertex in its own level and a vertex in the new level (denoted by 0). We can choose different vertices of the same "plugs" to connect to, so previous DP values are multiplied by number of such ways, like .

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

Can someone explain their greedy solutions for D with proof of correctness ?

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

    The comment above by Kareem_Mohamed is a common one FYI (basically it's explaining exactly Solution 2 in the tutorial, I don't know why it's downvoted :/). I've seen a few approaches equivalent to this, but I'm also curious whether someone came up with other insights and ended up in the same solution.

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

Above all thanks for the great DP round. XD

Here is one question. For problem E's sample O(n3) solution, there is a part of constructing from subproblem which says

"if (c2 > 2) add(ways[c0][c1][c2], (ll)ways[c0][c1 + 2][c2 — 3] * ((c2 — 1) * (c2 — 2) / 2)); // 2-2, 2-2"

As c1, c2, c0 are all limited under 50. How can we guarantee we always have future information [c1 + 2] when we need to calculate f[c0][c1][c2]? For example, when calculating f[1][49][30], we don't have the memo of f[1][51][27] cause c1 is larger than 50.

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

cyand1317 could you explain the dp states in D?

For a node u, does f[u][0][0] mean that u belongs to the first group and has depth%2 = 0?

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

I got an easy greedy solution for Problem D. Pretty straight forward 20 (actual) lines of code.

Submission

Basically take circles in decreasing order of radius. Find its immediate encompassing-circle. Then depending on the "tag" of that parent-circle, assign the "tag" for the current circle. Then add certain values of "tags".

Essentially I skipped the trees. And Dfs.

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

I'm having trouble understanding the problem D and it's example test cases. what does the area covered by an odd number of movement ranges of dancers who are moving. actually mean? how exactly is spaciousness calculated??

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

How would you do the bonus for problem D?