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

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

Several unexpected Kuhn solutions passed for D1F. Could you please discuss your solutions in the comments and prove its correctness or provide any counter-examples. Author's solution uses flows with Dinic.

Editorial is not completed yet. Problem D1F will be added later. Hope you enjoyed the problemset!

Editorial was/will be written by bthero and BledDest.

Our tester namanbansal013 has made amazing video-tutorials on YouTube for problems D2D/D1B and D2E/D1C. Make sure to check them out and show him some love!

Finally added the editorial for D1E. Currently it is very complicated and error-prone.

Div2A by bthero

Editorial
Code in C++ (BThero)

Div2B by nkamzabek

Editorial
Code in C++ (hugopm)

Div2C/Div1A by nkamzabek

Editorial
Code in C++ (BThero)

Div2D/Div1B by nkamzabek

Editorial
Code in C++ (BThero)

Div2E/Div1C by DimmyT

Editorial
Code in C++ (RedDreamer)

Div2F/Div1D by DimmyT

Editorial
Code in C++ (BThero)

Div1E by bthero

Editorial
Code in C++ (BThero)
Alternative solution code in C++ (hugopm)

Div1F by bthero

Editorial
Code in C++ (BThero)
Разбор задач Codeforces Round 673 (Div. 1)
Разбор задач Codeforces Round 673 (Div. 2)
  • Проголосовать: нравится
  • +248
  • Проголосовать: не нравится

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

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

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

thanks for instant editorial

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

D was really a good one! thank you for a good round :)

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

D was really a good one... Thank you for such an amazing round :)

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

Thanks for the super fast editorial!

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

Fastest editorial ever?

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

Can anyone explain C i still don't get it.

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

    Okay, here are some observations that might help you.

    • Consider element x, if they are at indexes 'i' and 'j'(consecutive occurrence of element x), then for a block of length [j — i — 1] or lower we cannot have that element as answer.
    • Also, if we have element x as answer for length 'L', then that element can be considered as answer for block of length >= 'L'.

    I think these two observations were enough for me to reach at solution.You can try too or I can explain if you are not able to reach to algorithm.

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

      Please elaborate more. It will be very helpful.

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

        Okay so here is what we can conclude from above observations.

        • As we are to check for consecutive occurrence of element x, It makes sense to make an array of positions for each element x from 1 to N.
        • So, now our question breaks down to finding maximum gap(or block of length that is invalid) between two positions for element x.
        • Do this for each element from 1 to N, and update their answer in array 'maxgap' which stores the minimum element x, that is valid answer for blocks of length > maxgap.
        • Now just traverse from 1 to N, print the minimum element you have stored till now for 1 to i'th in 'maxgap', to get answer and update the minimum element till now.

        Here is my code — Submission.

        I hope it helped.

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

    if x has to be the minimum number in all segments of length k then in input array between every occurrence of x and its previous occurrence there should not be another segment of length >=k. For instance in {1,2,2,1,1} for k=2 and x=1 we have a segment {2,2} of length two and hence not all segment s of length 2 have '1' in common. Note: We have to consider index -1 and n+1 also as value equal to chosen x.(segment from beginning and end);

    We can calculate this by maintaining a previous index for all the values we see traversing through array and updating the max distance between two same values. Then we can sort and find the min for every k.

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

Thanks for the quick editorial!

For div2b explanation, is the part "It is clear that f(X) = f(Y) = 0" a typo (since f(Z)=0, not f(Y))?

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

Loved Div2C. Purely algorithmic and appropriate difficulty.

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

.

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

    Okay, so let's make an observation if, for each distinct element, I know it's indices (could be stored by using a map to a set/vector), so let's say my current element is 'a' and it's distributed in the following manner in my array

    x x x a x x x x a x x a x a x x x

    (where x is an arbitrary element (x!=a) and a is our desired element)

    so now let's compute the maximum difference between consecutive indices and the distance of first 'a' from the start and the distance of last 'a' from the end.

    The consecutive differences are

    [4 (dist. from start), 5, 3, 2, 4 (dist. from end)]

    now the max diff from the above array is 5, so now for every sliding window of length 5 & more, I can ensure that at least one 'a' is present in each of the sliding window. (Can easily be visualized)

    Thus, now as in an ordered_map, it's sorted according to its keys, so for each element, when I calculate the max diff for the indices of this element (let's say it's y), I can mark the answer for all length from y to n, as my current element. In the above loop (used for marking my answer), I would iterate only till a point where my answer isn't marked (as whenever I encounter a marked index, all the indices from that index to n would already be marked & as it's a map, it would definitely be marked with a smaller element!), and thus after that, I break the loop.

    For all the indices that aren't marked, the answer would be -1.

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

Isn't the solution for 1B missing something ? The statement says that after each operation, all elements of the array should be non-negative. Edit: I now see that $$$a_1 \ge i-1$$$ at the $$$i$$$-th iteration.

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

    I think that this solution takes that into account. All array elements start greater or equal to 1. If we start accumulating the sum at $$$a[2]$$$, then we are guaranteed to at least add 2 to $$$a[1]$$$, meaning $$$a[1] \geq 2$$$. Then even if $$$a[3]\mod 3 \equiv 1$$$, it is guaranteed that we are able to add 2 to bring it to a multiple of 3, and turn $$$a[3]$$$ into 0. In this way the entire sum can be moved into $$$a[1]$$$.

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

Dimmmy must've used his time travel capabilities again to bring such a fast editorial!

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

Div2D/Div1B

"Otherwise, we have to make it divisible by transferring i−(ai mod i) from a1 to ai. Note that this operation does not break a condition on non-negativity because all ai are initially positive."

Why does this not break the condition? How are we guaranteed that a1 >= i-(ai mod i)?

EDIT: just figured it out after posting this comment. Since we iterate in increasing order of i, each element we've seen will contribute at least 1, so by the time we are at index i, a1 will have at least (i-1).

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

    Imagine you are at position pos and have successfully accumulated all 2..pos-1 piles into the pile at position 1. Since v[i] for all i is at least 1 you'll have at least pos-1 value in the first position. Notice that v[pos]%pos can be at max at pos-1. (although pos-(v[i]%pos) can be equal to pos but that case is handled separately). Hence if we iterate from left to right it'll always be possible to do this.

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

    Basically at each 'i' , subtract at max (i-1) from 1, and add atleasr 'i' at 1 , hence a[0]+= (1 or more for each i). Then as robinz62 edit.

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

i think that In Div2E/Div1C editorial let's construct a trie on our array integers, trie is typo and tree is correct meaning.

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

That was too fast. Thanks!

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

Solved C quickly but failed on B. I think it a rare condition in this round.

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

The elegancy of the solutions made me happy.

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

Div2C is a very elegant solution

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

In Div2D/Div1B, is it possible to use a ternary search to find the value we want all the elements in the list to assume?

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

"Otherwise, there always exists a solution which uses no more than 3n queries." What is the solution of [0,1,1,1,2], please? sum=5,divisible by n=5.

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

Div2D

Why can't we just take $$$\frac{a_i}{i} * i$$$ from each $$$2 \leq i \leq n$$$, making values equal to $$$a_i := a_i\mod i$$$ and transfer it to $$$a_1$$$, using exactly $$$n - 1$$$ operations, then transfer needed values back from $$$a_1$$$ to all elements, using another $$$n-1$$$ operations?

I've desperately tried to find some counter-exapmle during last 1.5 hours, but I couldn't.

UPD: Got it, thank you all

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

Can anyone explain div2C problem in simpler way with some example

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

    Consider the distance between 2 positions (j > i), as d = (j — i — 1).

    For each number x that may be present in the array, calculate the following distances:

    1- Between the first element of the array and the position of the first occurrence of x.

    2- Between pairs of positions (j > i), such that a [i] = x and j is the position of the element equal to x closest to i on right.

    3- Between the last element of the array and the position of the last occurrence of x.

    Now take the largest of these values for each x value, consider this value equal to k.

    The number x appears in all subsegments of size >= k.

    then we have the answer :)

    my code: https://mirror.codeforces.com/contest/1417/submission/93998385

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

Nice div1C

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

Also, thanks for strong pretests <3

.

UPD: I really don't understand this contests with 3 pretests.

UPD2: I actually understand why we don't have full tests. But when we have so weak pretests, contests are just BlindForces. Why don't remove all pretests at all in this case? At least participants will get same experience, IMO

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

Tutorial for Div2C is hard to understand. I solved Div2C, maybe the way the tutorial explains it, I am not sure.

So I think if somebody did not solve it, it is completly not understandable. What is meant by "we can update the suffix [some formular]..."?

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

    For every array element x, they compute the minimum window size k which is guaranteed to contain x. If the minimum k contains x, then all window sizes greater than k also contain x. Then, they sort the array elements in increasing order. For every array element, they have computed a minimum k which it covers. So we try to update the suffix [k..n] with this array element. If we find some k' in the suffix [k..n] which is already updated, then we don't need to update any further because every element in the suffix [k', n] has already been updated by a smaller array element because we're iterating through the elements in increasing order.

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

      How to update "the suffix", whatever it is?

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

        We make an array which maps the window size k to the smallest number which occurs in all windows of size k. For example if the array size is 3, then we will have 3 window sizes. So, let the array(1-indexed) "K" be [-1, -1, -1]. Then, we sort the array given in the problem in non-decreasing order. Let's say the array given in the problem is [1, 2, 3](already sorted). If it wasn't, then sort it.Let's also assume that we have computed the smallest window size k for each of these elements. For 1, this window size is 3. For 2 this window size is 2. For 3 this window size is 3.

        Now, we will update the suffixes of the "K" array while iterating through the array we sorted.

        When iterating through the array we sorted, we will run into 1, 2, 3 in that order. For 1, the smallest window which works is 3. So, we will update each k >= 3 in the K array with 1. Now the K array is [-1, -1, 1].

        Then we'll run into 2 while iterating. The best minimum window size which works is 2. So, we'll update the "suffix" with indices [2..3] in the K array with the value 2. But, when we try to update position 3, we see that it has already been updated. So, we can stop and we don't need to go any further. Now the "K" array is [-1, 2, 1].

        Then we run into 3 while iterating. The smallest window which works is 3. So, we can update the suffix [3..3], but it has already been updated by a smaller value 1. So, we can stop.

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

Can someone tell me how I passed pretest and managed to get runtime error on test "1" during system testing?

here is my submission: 93984302

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

This contest made me purple for the first time :) Thanks for the good problems

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

On this contest I had big troubles with problem E. I used the idea with the mergesort + checking every bit of answer from last to first. It uses O(30 * n * logn) time, and with n = 3e5 it is approximately 1e8, is it really that slow for time limit = 2 seconds? If anyone can get AC with my code, please share my mistake. (94018657)

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

    My solution was with almost the same idea as yours and also got TLE on test case 12, when I optimized my code it runned locally on 3 seconds for the worst test case.

    However I think you should use long long for computing the amount of inversions.

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

a great contest but very weak pre test for B (div 2)

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

Div2-B my code What's wrong in this I'm unable to figure it out. If anyone could help me. Initially it is assigning all the elements of array as '0' and putting current index(j) in map if it's not making 'T' with another previous element i<j, but when it founds a[i]+a[j]==T 'j' is assigned to 1.

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

Testcases were very weak for Div2 C. I saw a lot of submissions where the test case 1 1 3 would fail, but somehow got accepted. I submitted my code at 1h 6 mins(all pretests passed). Went on ahead to try D, but at 1h 52 mins, I realised, that I've missed the case for n = 1, So I resubmitted it. Turns out they didn't have a basic case like n = 1. My ranking fell from expected 1200 to 1765. Please do something about the resubmission rule.

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

When you submit solution in the last min and net fucks you up and then you submit that after contest and it ACs. FML

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

Weak Test cases for problem (div.2)A. A soluion with 10^7 operations(in worst case) in java, it just has 3 test cases which are also randomly made i guess. look at this solution. I know its just problem A but still it decreases the quality of contest.

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

"Several unexpected Kuhn solutions passed for D1F. Could you please discuss your solutions in the comments and prove its correctness or provide any counter-examples. Author's solution uses flows with Dinic."

Don't worry, it is almost impossible to hack Kuhn (especially on those, specific graphs). Even if it is possible, it is VERY hard. Of course, I don't have any proof of my words, but I know it from my experience.

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

    This is a strange question, but: how to solve this problem with Kuhn? I don't doubt its time efficiency, but I don't know how to find the maximum matching which saturates some given set of vertices without using circulations (which is the model solution).

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

      Alright, so you need to find the matching that covers some set of vertices.

      • Using Kuhn in proper order, find any maximum matching that covers all vertices of the left part from the set, let's say that the set of covered vertices of the left part is $$$L$$$ (some vertices from the set + something else).

      • Using Kuhn in proper order, find any maximum matching that covers all vertices of the right part from the set. Similarly, define $$$R$$$.

      The claim is: there is a maximum matching on the set of vertices $$$L \cup R$$$.

      You can prove it using alternating paths.

      And then you can just leave only those vertices and find the maximum matching.

      Note: this algorithm also can be used for solving the maximum vertex-weighted matching where you have weights on both parts. I will leave it to you as an exercise.

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

      Another way (I didn't participate today, though, so I'm not 100% sure). We can model our subproblem (given a bipartite graph, find a matching covering a given subset of vertices) as a maximum-cost flow: connect a source with all vertices of one side of the bipartite graph with oriented edges with capacity 1 and cost 0 or 1 (1 if the vertex must be covered by the matching). We analogously connect all vertices of the other part of the graph with the sink. Now, if we need to cover $$$R$$$ vertices, we can easily see that we are looking for any flow with cost $$$\geq R$$$.

      But running a maximum-cost flow algorithm naively is probably too slow. In order to cope with that, we need to understand how the simplest maximum-cost flow algorithm works on our network:

      • In the first phase, the algorithm iteratively finds augmenting paths with a total cost of $$$2$$$. We can convince ourselves that this is actually equivalent to finding the maximum-size matching on the subgraph induced by all required vertices (and no other vertices). We can do just that using Kuhn's algorithm.
      • In the second phase, the algorithm iteratively finds augmenting paths with a total cost of $$$1$$$ in the residual network created after the first step; hence, we are looking for alternating paths connecting an unmatched required vertex with an unmatched non-required vertex. This is equivalent to running the Kuhn's algorithm on the graph above (with the matching found in the first phase), only that we only search for alternating paths originating from a required vertex (remember that the origin can belong to either part of the graph).
      • No further augmenting paths in the max-cost flow algorithm will increase the cost of the flow, so we're done — we found a matching covering as many vertices as possible.
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi, Can anyone please tell me the time complexity of my code for D2B, according to me, it is approximately O(N log N). But it gave TLE on the system tests, which shows that I have approximated the complexity incorrectly. Here is the link to my submission:

https://mirror.codeforces.com/contest/1417/submission/93976053

Thanks in advance!

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

    If input looks like $$$a = [x,x,x,...,y,y,y,...]$$$ and $$$x + y = T$$$, every time your program sees a $$$x$$$ it will go through every positions of $$$y$$$ and set the answer, then the run time becomes $$$O(n^2logn)$$$

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

For problem D how can it be told for sure that a[1] wont be negative after the operation. lets say if a[i]=8 and i=7 then it would take 7-(8%7)=6 at least 6 but if a[1] is less that that how what will happen then? what am i missing?

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

Aaaaaaaah! That feeling when a solution passes after contest by changing just ONE CHARACTER!!!!

Nice contest!

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

please someone help what's wrong in my code in div.2(B)? It failed in system testing(test#4). https://mirror.codeforces.com/contest/1417/submission/93983911

why I am downvoted why it's not right of the "pupil" to ask doubts

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

My solution for Div1 D using small-to-large merging passed in 1.4s. 94011971

The idea is that we simulate small-to-large merging for edges in reverse order and save which values were in a smaller set, so we could simulate the process in reverse order by removing the smaller set from the larger set.

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

In DIV2B , why can't we just put all pairs whose sum add up to the target into different multi sets?

 for(int i = 0 ; i < n ; ++i){
        
        int curr = arr[i];
        int val = tar - curr;
        
        if(freq.count(curr) <= 0) continue;
        --freq[curr];
        if(freq[curr] <= 0) freq.erase(curr);
        
        if(freq.count(val) == 0){
            ms1.insert(curr);
            continue;
        }
        
        if(ms1.count(val) > ms2.count(val)){
            ms1.insert(val);
            ms2.insert(curr);
            --freq[val];
        }
        else{
            ms1.insert(curr);
            ms2.insert(val);
            --freq[val];
        }
        
        if(freq[val] <= 0) freq.erase(val);
        
        
        
    }
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

Was O(nlog(n)log(max(A[i]a)) not intended for div2E or does my solution just have poor constant/ bad implementation on that time complexity?

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

Otherwise, we have to make it divisible by transferring i−(aimodi) from a1 to ai. Note that this operation does not break a condition on non-negativity because all ai are initially positive. I don't know why this sentence is true. If a1 is small, isn't it a negative number? Or am I too stupid?

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

Rating changes working correctly? 2000 rank made me a specialist again

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

Can any one give a formal proof for Div 2 A?

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

Can someone please explain div2b, how we can separate array a in white and black balls based on given unlucky integers T?

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

Some video solutions (for 2A-F), in case you like those

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

I might be wrong here, but isn't your solution for D wrong with an input of:

1
8
0 0 2 0 0 0 0 6

The second operation from your code 1 3 1 would reduce the first element by 1 and increment the third element by 1, but the first element is still 0 at that stage.

16
2 1 0
1 3 1
... // snipped here, got the output from a custom invocation call
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

For Div1C / Div2E I have another solution without using a trie.

Here is my submission: 94030812

Explanation:

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

can someone tell me what is wrong with my solution....what i did was take input as pairs(to remember the index after sorting by value) and loop until i find two index that gives the sum "k" and the remember the indices of the two value. Now just make half of the subarray 1 and others zero then sort by indices to finally print the answer. submission : 94032531

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

For div2 B it shows WA, My solution was to divide them like this: paint X white and T-X black if it exists and distribute all x=T/2 equally to black and white. What is wrong with this approach.

My Solution: https://mirror.codeforces.com/contest/1417/submission/93987002

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

Div1 C TL not friendly :( My solution with hash table didn't pass.

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

Is div2D solution wrong? consider 1 1 1 1 3 3 4 the sum is divisible by 7, but it seems to be unsolvable...... pls help me with this case :)

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

In Div2B if we consider this test case

5 9
1 3 3 3 8

According to solution provided it would be 0 0 0 0 1

But optimal solution would be 0 0 0 1 1

Am I correct nkamzabek ?

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

In Div1 B , the condition:"$$$a_i\geq 1$$$" is really important.At first,I ignore it,so I am stuck for a very long time.

Compared to B,Div1 C is much classical.

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

According to problem Div2D: Number 1 is the most powerful number :))

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

Construct a Trie to solve Problem E. I can never think such a great idea! Here's my solution: 94045226 without using trie. Code may be a little long, but it got accepted!

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

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

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

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

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

Div 2B: Even after reading editorials I don't see any error. Can someone plz tell me what's wrong with my code? Solution B. My solution was to divide them like this: paint X white and T-X black if it exists and distribute all x=T/2 equally to black and white.

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

Is the rating change unusual this time for everyone or only me?

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

We (me, gyh20, Time_tears) have a totally different (and much easier, we think) solution to D1D.

First add the edges from the last operation to the first, and at the same time use disjoint-set-unions (with merging the small one into the big one) to add the numbers a connected component contain into a vector and keep the total size under $$$n\log n$$$.

After getting done with the vectors, sort each of them from small to big. Let $$$bel[i]$$$ be the connected component the point with value $$$i$$$ is now in. For all $$$i$$$ from $$$1$$$ to $$$n$$$, let $$$bel[a_i]\leftarrow getfather(i)$$$. (getfather is the dsu function)

Then we answer the queries:

  • If this operation is a deleting operation, let's assume that we delete (x,y). If deleting the edge doesn't affect connecting components, ignore it. Else let $$$getfather(x)\to x, getfather(y)\to y$$$. Assume that $$$size[x] \lt size[y]$$$ (size of connected components after deleting). For all points $$$i$$$ in x's component, let $$$bel[a_i]\leftarrow x$$$. Also change the father of $$$x$$$ to itself.
  • Else, let $$$getfather(x)\to x$$$. Consider from back to front in its vector: if the number is printed, pop it (use pop_back). If it's $$$bel$$$ isn't x, pop it. Else print it.

The solution runs a lot faster than the author's. Without any optimizes, it only uses 452ms. Also it doesn't need any data structures.

My code is here: 94056232

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

why my O(30*n) solution giving TLE in E

94058521

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

can anyone explain what does this line in editorial for DIV2 F mean, If the current query is of first type, remember the "boss" of the corresponding vertex. How is the question transformed to subtree-maximum query on DSU tree ?

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

why my O(n*30) solution in E give TLE

94058521

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

Can Someone please tell me what's wrong in my solution for div2-B

https://mirror.codeforces.com/contest/1417/submission/94138194

I have simply changes the color (k — number) to opposite value of the (number). This also ensures that all the n/2 numbers are equally distributed but still getting wa on test 4

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

The editorial of Div1E is missing. Could anyone share the idea of its solution?

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

Since the editorials of problem E and F are missing, I will share my approach to solve them:

E:

Observation 1
Observation 2
Observation 3
Observation 4

F:

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

In the editorial of Div2E/Div1C,there's something I can't understand. In the tutorial, $$$a, b$$$ are the children of $$$v$$$, and $$$v$$$ has a depth of $$$k$$$. Then $$$a, b$$$ have a depth of $$$k-1$$$, and the highest bit which differs in both should be the $$$k-1$$$-th highest bit. So I think only the $$$k-1$$$-th highest bit of X is toggled, lists $$$S(a)$$$ and $$$S(b)$$$ will change their relative order, not $$$k$$$-th. Can anyone explain it?

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

Edit: has been fixed.

The maxn (4e6 + 100) in the code of Div2E/Div1C is small and can be hacked by this generator:

#include <bits/stdc++.h>

using namespace std;

const int mx = 1e9;
const int n = 3e5;

int main() {
   cout << n << endl;
   int cnt = 0;
   for (int i = 0; (i << 12) <= mx; i++) {
      if (i > 0) {
         cout << " ";
      }
      cout << (i << 12);
      cnt++;
   }
   for (int i = 0; cnt < n; i++) {
      cout << " " << ((i << 1 | 1) << 11);
      cnt++;
   }
   cout << endl;
   return 0;
}
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Nice contest. I'm new to the technique of adding fake node to build dsu tree where used in Div2F/Div1D problem. Is there any relevant problem can be solved by this technique?

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

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

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

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

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

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

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

Div2 A. The statement "any operation which increases the value of $$$min(a_1,a_2,…,a_n)$$$ is unoptimal" (the proper English word is non-optimal) is false. Besides, it is meaningless since not every operation that preserves the minimal value is optimal.

1
2 3
1 2

You can copy once regardless of whether you copy from $$$a_1$$$ to $$$a_2$$$ or from $$$a_2$$$ to $$$a_1$$$.

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

Thanks for Editorial ,But I think in problem Div2D the code will fail if testcase is 4 4 1 0 1 2

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

Thanks for Editorial

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

seeing space complexity for the first time in editorial!! one of the best round.

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

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

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

I am thrilled after understanding div2 F's solution

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

In the problem Div2 D, the editorial said that transfering i-(a[i]%i) from a[1] to a[i] does break the non-negative rule. How about the case that a[1] < i(a[i]%i)? Will the a[1] become negative?

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

In Div2E/Div1C we can convert all numbers to binary. Then we start with the whole array of n elements and look at the most significant bit. Iterate over the array, counting inversions and antiinversions along the way. (Inversions are 1-0 pairs and antiinversions are 0-1 pairs, 0-0 and 1-1 pairs are neither.) Split the array into two subsequences, placing the elements with a 0 bit in this position to one of them and elements with 1 bit to the other, while preserving order within each subsequence. Repeat the same procedure recursively on each of the subsequences, with the next most significant bit. Inversions (as well as antiinversions) at the same recursion depth should be added up. Recursion stops, after processing the least significant bit. At the end we have the total number of inversions and total number of antiinversions per recursion level i.e. per bit position. Now we can construct the required x, setting its bits to 1 at positions where the number of inversions is strictly larger than the number of antiinversions and to 0 in the others. Complexity is O(n logn).

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

Unofficial Div1F editorial (Dinic's maxflow solution):

Note that a satisfactory solution can be produced as follows: for every cell, exactly one of the following must be true:

  • It points to a neighbor of lower value than it that doesn't point back, and its cost will be the difference between the values.
  • It points to a neighbor of equal value that points back to it. The sum of their costs will be equal to their shared value (division can be arbitrary, e.g. $$$(1, x-1)$$$ or $$$(\lfloor x/2 \rfloor, \lceil x/2 \rceil)$$$)

Note that the second condition resembles the bipartite matching problem that arises from domino-tiling an arbitrarily-shaped chessboard (e.g. this problem from AtCoder). In fact, it can be framed as a bipartite matching, but with only a subset of nodes being mandatory participants for the matching. A node is mandatory iff its cell has no neighbors of lower value, otherwise it's optional.

Recall that to use Dinic's maximum flow to solve a bipartite matching, there are source-edges to one of the parts (the "white" cells on a chessboard), sink-edges from the other of the parts (the "black" cells), and directed edges from the first part to the second part based on potential matchings (i.e. between cells with equal value). To model mandatory nodes, we add a "demand" to its source or sink edge, modeling demands by the transformation detailed in this tutorial.

The answer is yes iff all demands are met by the flow. The solution can be reconstructed by matching cells by the second condition if there is flow between them. If a cell isn't matched this way, the first condition can be fulfilled instead; this is always possible as the node would have been marked mandatory otherwise.

Beware: Dinic's algorithm implementations that pass many other flow problems fine may TLE here due to a subtle bug. In particular, ensure your adjacency-list pointers (ptr in the author's solution, or next in my solutions [recursive | iterative]) only increment if its edge is exhausted, i.e. the search in that node hasn't fulfilled its flow limit yet.

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

In problem 1D, the given solution code runs about 1.3s (GNU C++17(64)) and the TL is 1.5s?

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

All spoilers are broken. Please fix this.

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

I have alternate solution for div-2 F which has same TC as the editorial's solution, but slightly worse space complexity, $$$(O(n + q)\log{n})$$$.

Firstly, store all the queries, and delete all the edges given in the type-2 queries. Now, in this new graph, unite all the nodes in connected components into one dsu set each.

Now we will basically try to simulate the process in reverse. We can do it like this: Iterate from the last query to the first. Now all the type 2 queries are essentially unite queries (as we are going backwards), so store the set representatives of both the ends of the edge that was to be deleted in this query. Also, store the smaller set.

Now we will iterate from first to the last query, and answer queries too.

  1. For type 1 queries: simply check max in current dsu set of given vertice, set its value to 0, and update dsu set accordingly.
  2. For type 2 queries: we had earlier stored the smaller set that was merged into the larger set. So now we must separate that smaller set from the larger one. But some elements of the smaller set might have changed in the prior type-1 queries. So we can just check the new value of each element before adding stuff back into the smaller set.

Implementation: link