When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

vovuh's blog

By vovuh, history, 6 years ago, translation, In English

1042A - Benches

Tutorial
Solution

1042B - Vitamins

Tutorial
Solution

1042C - Array Product

Tutorial
Solution

1042D - Petya and Array

Tutorial
Solution

1042E - Vasya and Magic Matrix

Tutorial
Solution

1042F - Leaf Sets

Tutorial
Solution (O(n log n))
Solution (Small to Large, O(n log^2 n))
Solution (O(n))
  • Vote: I like it
  • +57
  • Vote: I do not like it

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

so fast :/

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

gone in a flash :o

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

Wow, I failed C because of writing >= 2 instead of >= 1

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

Loving the quick editorials !

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

F*cking Monikaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa........

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

Back to Back contest. And Back to Back fast editorial. I'm loving it.

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

I'm loving quick editorials. Really helpful.

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

    Hey can you please explain the line "It's easy to see that the answer for the fixed L is n∑R=Lpref[R]<t+pref[L−1]. We can calculate this formula using some data structure which allows us to get the number of elements less than given and set the value at some position. "?

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

      I didn't understand that Question's Editorial much.(Mostly because i have little experience with BIT and Segment trees).

      I did that question D after the contest using a modification of the merge sort(I read that in a comment somewhere). You can read this comment It might be helpful.

      But overall you need to learn BIT or Segment tree to make sense of the editorial at all.

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

        Thanks a lot :) . In that comment the algorithm used is BIT right? and not merge sort?

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

For F,(untested)

I was thinking of fixing the centers of subtree formed by the leaves. For fixing centers, we greedily move from lowest to highest depth(from bottom to top) and pick centers when necessary. Also to avoid problems, we can assume k is even (If not add some dummy vertices and make it even).Now from a center we want distance to farthest not yet taken leaf node to be k/2 (which is what I mean by when necessary).

Does this work?

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

    i think it works but it's hard to code(for me)

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

Got TLE on tc 68 in C, DAAH! :(((((((((((((((( .

.

.

.

.

.

.

Because of cin,cout DOuble DAAH! :(((((((((((((((((((

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

Can anyone explain me solution to Problem D?

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

    Let's say the input is A[1...n]. You first take the cumulative sum of A. Let this sum is S[1...N].

    So by the property of S, you know that the summation of any subarray from j + 1 to i is S[i] - S[j], right?

    Now, for each i, you want to find out how many j < i are there such that S[i] - S[j] < t. We rewrite the above as S[i] - t < S[j] i.e S[j] > S[i] - t.

    So, for each i, you take S[i] - t and find number of previous values S[j] where j < i. This can be done in Segment Tree or Fenwick Tree by doing coordinate compression, or simply you can use C++11 Order Statistics Tree from gnu pbds.

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

      Thanks!

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

      How to apply coordinate compression in segment tree in this problem? I wrote a solution with only segment tree and got TLE. 43006558

      Update: I had made a big mistake. Solved using this quora ans about KQUERY

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

      my code with Order Statistics Tree, makes the code very simple: 43010933

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

        can you pls explain the second parameter in order_of_key?

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

          During the implementation I relized that a multiset is needed, in order to store more than one instance of key in the tree. So I gave every key anoter parameter that distinguishes it from the others and doesn't affect the order of the actual values.

          It's a nice trick, if someone has a better solution to make it a multiset please share.

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

      Great explanation, but I think I lost myself in some point. How this algorithm would work on this test case ?

      3 0 -1 2 -3

      The cumulative sum would be?

      -1 1 -2

      If yes, just in the -2 I would find 2 S[j] greater than -2. In the other two I don't find any.

      The answer for this test case is 4.

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

        NO, the partial sum, in that case, would be,

        0, -1, 1, -2.

        This might clear your doubt

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

    you can also use a merge sort tree on prefix sums and binary search after.

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

can someone help me generate some hacks for my C solution 42986208 it looks quite similar to editorial

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

In F, I get AC by following greedy algorithm but I don't know why it can get AC:

Let the center of the tree is vertex c.

Let the leaves are x1, x2, ...xm and dis(x1, c) ≥ dis(x2, c) ≥ ... ≥ dis(xm, c).

for i from 1 to m:
    if x_i is not selected, make all vertices y that are not selected and dis(y,x_i) <= k be a beautiful set.

Can someone prove or disprove it >__<?

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

    http://codeforces.com/contest/1042/submission/42991099

    This seems to be O(n^2) worst case (calling f() multiple times), do you know why it isn't?

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

      The time complexity of my solution is .

      When , each vertex will be visited at most k times because I write if(r<=ma[x])return; and ma[x] is range from 0 to k. Thus, the function f only visit k × n state.

      When , a tree has at most O(n / k) leaves such that their distance is over k each other. So we only call function f at most times.

      I use the tightest tests I can think out to test my program and it takes about 2.6 s in Codeforces to calculate the solution.

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

    I think it is correct.

    In your algorithm, you find leaf x with maximum distance from the root c first. Which means one of the longest path of the tree must start at node x. We assume path(x, u) is the longest path of the tree.

    Then, consider all y where dis(y, x) <= k. It is true that dis(y_i, y_j) <= k as well. Otherwise both dis(y_i, u) and dis(y_j, u) > dis(x, u) which is not possible.

    Therefore, the beautiful set found by your algorithm should be valid. After you found one beautiful set, you ignore those nodes in further processing, which is similar to delete the subtree containing those nodes. Therefore, all the beautiful set found by your algorithm should be valid as well.

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

      I get that the sets made by the algorithm are valid but why the total number of sets is minimum? You mean if our main vertex was not the center of the tree the algorithm would be correct either way? sorry if i asked a stupid question. sorry for my poor English.

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

        Let the first node (the node use for comparing the distance) we pick for each set be x_1, x_2, x_3 ... x_m

        According to the algorithm, It must be true that dis(x_i, x_j) > k otherwise they will be put to the same set. Therefore the algorithm should attain the optimal answer.

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

IN array product my answer fails on 7 th test case . but i think my code is right.

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

    Try this test case:

    6

    -8 -9 -7 2 3 0

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

      yes. Now I got it.

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

      my output for this test case is
      1 5 4
      1 3 6
      2 6
      1 1 2
      1 2 4

      which in turn equals to 432(-8*-9*2*3). but still i am not passing test case 7 . according to you what will be its answer?.

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

http://codeforces.com/contest/1042/submission/42995015

Please, Can someone see my solution? Codeforces says wrong answer but it is producing output as expected.

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

    Your logic is incorrect, you are forgetting that you cannot move the people who have occupied the benches, hence not necessarily you can make equal people sit on all the benches to find the minimum.

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

    I think your solution is wrong

    Here is the sample 2 3 1 10

    The answer is 10 13 Your code output 7 13

    Cause the three people can sit on the second bench

    So for each time where not all the people were sat , should find the min people of all bench and let this person sit here

    ( people who is already sat can not move , maybe you should read this problem again :)

    Hope this can help you

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

Am I the only one who thought of DP at the first glance of B?

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

    I also solved it using DP.

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

      Can you please explain the solution and the dp state you chose, I couldn't understand your solution.

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

        see mine, it should be easier to understand.

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

          Your approach seems quite interesting. Can you please explain it? I could not figure it out. Just a brief explanation of what you did.

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

            list the state in 3-bit binary number

            if the state includes the bit 001 it means it contains vitamin A

            if the state includes the bit 010 it means it contains vitamin B

            if the state includes the bit 100 it means it contains vitamin C

            The answer is ofc dp[(1 << 3) — 1].

            The rest is just simply knapsack

            This technique is called bitmask DP if ure interested

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

              Cool, the dp solution in prob B is quite elegant in my opinion. I implement dp too, btw my implementation is quite awkward

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

Could anyone please explain for me the basic idea of F. It is not so vivid to me. I understand the technique here but the idea....

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

By the way, how to write the judge code for problem C? (I think this is not trivial.) Does anyone have an idea?

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

    I can give you both checkers for this problem if you need :)

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

      Oh, I missed the last paragraph of tutorial C (very sorry...!). And I'm interested in both checkers. Could you give me these sources...?

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

Why does this D solution gives TLE on 46 test case?

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

    It seems that the complexity is O(N*logN*logN)

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

      How? When I'm iterating with i, every time I call 2 log(4e14) functions. So complexity is O(2*N*log(4e14))=O(N*log(4e14)).

      log(4e14)=14. Did I miss something?

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

        In query(ll k) and add(ll k) functions, you used for loop in O(logN) time. For each k, you used map, the complexity was O(logN) time then total complexity for each function is O(logN * logN)

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

I have another solution for problem D(Petya and Array) which doesn't need any data structure and I think it is really easier. It's using the merge sort.

let the given array be a. we make array A such that A[0]=0 and for i>0 A[i]=A[i-1]+a[i-1]. (it's the partial sum!)

now we must find number of pairs i,j (0<=i<j<=n) such that A[j]-A[i]<t . now we run merge sort on array A this way:

void Sort(int l,int r){
    if (r-l<2) return ;
    int mid=(l+r+1)/2;
    Sort(l,mid);
    Sort(mid,r);
    for (int i=l; i<mid; i++){
	ans+=(lower_bound(A+mid, A+r, A[i]+t)-A) - mid;
    }
    merge(l, mid, r);
}

explanation: in fact I am using divide and conquer. this algorithm at each time splits the given array into two arrays and counts the number of such pairs and sorts them. then it counts the number of pairs between the two sub arrays. and to count the number of pairs between two sub arrays it does the following: first we know the two arrays are sorted (we are using merge sort!); so we can use binary search on them! for each element A[i] in the first piece we can count the number of elements A[j] such that A[i]+t<A[j] using binary search!

time complexity of this algorithm is O(n * log^2(n)). my code: 43010896 It got AC in 156 ms. (I think if I have used scanf instead of cin it would be also faster) :)

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

    It can be further reduced to O(n*logn) by calculation while merging. My Soln .

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

      but your solution got AC in 233 ms and mine in 156 ms ;)

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

        Because the constant factor of my algorithm is more than your algorithm. I copied elements and created more vectors than yours, whereas you have only swapped them. And for calculation of time complexity, we generally ignore constant factors.

        Edit — I have removed some unnecessary commands including clock() and switch to array (which you have used) instead of vectors. Now it takes 124 ms.

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

Can any one explain me what am I doing wrong in question C? I am continuously getting wrong answer verdict on test case 7.

My solution link is:-

http://codeforces.com/contest/1042/submission/43011863

Thanks in advance

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

    If you have odd number of negative values and one zero element then you are multiplying all negative value with zero insted of just multiply maximum value of negative with zero and remove it. I think in this test case you are getting wrong answer.

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

About Problem E, what is the "real" answer for sample case 2 (that can be represented by a rational number P/Q?

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

    It is .

    There can be some paths, with their respective possibilities:

    • (1, 2) → (1, 1) — probability of , final score of 1

    • (1, 2) → (2, 3) — probability of , final score of 2

    • (1, 2) → (2, 1) → (1, 1) — probability of , final score of 3

    • (1, 2) → (2, 1) → (2, 3) — probability of , final score of 6

    • (1, 2) → (2, 2) → (1, 1) — probability of , final score of 3

    • (1, 2) → (2, 2) → (2, 3) — probability of , final score of 2

    • (1, 2) → (2, 2) → (2, 1) → (1, 1) — probability of , final score of 3

    • (1, 2) → (2, 2) → (2, 1) → (2, 3) — probability of , final score of 6

    To summarize, the expected answer will be: .

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

      Hey Akikaze, can you tell me what evi is, in the tutorial of E?

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

      Why can't we calculate for [r, c] all the possible jumps from it to all the elements with value < val[r, c]? we ((sum them up) / number of such elements < val[r,c])? And similarly for all the other elements < val[r, c], summing them up until we no more find element < val[i, j]?
      (By summing them up, I mean summing the euclidean distance between them.)
      Here is my code. Would be awesome if somebody cleared this doubt.

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

Failed on tooooo many troubles. Sad.

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

can someone explain the part in D's editorial how segment tree or fenwick tree can be used

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

For the problem D, anyone can explain more details for me please. What do we do when call upd(k)?

int npos = upper_bound(sums.begin(), sums.end(), pr - T) - sums.begin();
ans += (i + 1 - get(npos - 1));
		
int k = lower_bound(sums.begin(), sums.end(), pr) - sums.begin();
upd(k);
  • »
    »
    6 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    It's actually bad editorial. It's hard to understand ( cost me one day )

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 4   Vote: I like it +14 Vote: I do not like it

      This is similar to the Inversion number problem, formally:

      Given an integer sequence A which consists of N elements, count the number of pair (i, j) where i < j and A[i] > A[j] are satisfied.

      It is known that this problem can be solved with BIT. The strategy is like:

      1. S = [], ans = 0
      2. iterate j from 0 to N - 1, and do A), B), C).
      3. A) calculate the number of elements in S which is less than A[j]. Let this number be x.
      4. B) add j-x to ans
      5. C) add A[j] to S
      6. print ans

      The important part is A). if you count this value with straight forward algorithm, it will work in O(number of elements in S). To get this value efficiently, we can use BIT.

      BIT provides these operations:

      1. add(i, x): v[i] += x; (in the tutorial code, upd(i) := add(i, 1) )
      2. get(i): return v[0] + v[1] + ... + v[i]

      O(N) memory is required and both operation can be done in time (where N is max i).

      Treating A[j] as i, we can perform A) and C) operations using BIT, like A): get(A[j]), C): add(A[j], 1)

      But there is a problem: A[j] can be big and it will cause MLE...

      So, we have to pre-calculate k, such that A[j] is the k-th smallest number in A. (This can be done with sorting, unique the array and calling lower_bound)

      After that, we can use k instead of A[j] for BIT.

      This solution works in where N is the number of elements in A.

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

        Can you provide the simpler code using BIT for this question D?

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

Solution (O(n log n)) has some problem for this data.

14 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14

this solution's answer is 2.
this is incorrect, yes?

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

    There are two leafs(1 and 14) and distance between them is more than 1, so answer is 2.

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

      thanks,I have a wrong Understanding for this question.

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

I solve D with wavelet tree, then realise the operation I use can be done with simple coordinate compression and fenwick tree.

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

Problem F, the constant of O(n) solution is so large that the real runtime between O(n) and O(nlogn) is similar. (1076ms for O(n), 1091ms for O(nlogn))

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

Every time I read the editorials I think I need to put lot more effort

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

Problem D can be solved with STL's red-black tree implementation (https://codeforces.com/blog/entry/11080): http://codeforces.com/contest/1042/submission/43157263.

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

Can someone please tell me what is the bug in my code (http://codeforces.com/contest/1042/submission/43167869) for problem D. I used sqrt decomposition technique and am not able to find any bug myself. My logic is basically to find all the r>=l for every l.

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

    You have to check whether j is valid or not. (It is not guaranteed that (b+1)*M is less than n+1)

    see: https://codeforces.com/contest/1042/submission/43174792

    UPD: If you use g++, passing -fsanitize=undefined and -D_GLIBCXX_DEBUG options to the compiler makes it easier to detect these types of bugs (I found the bug just to try Sample Input 3. Please try it!)

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

      Thanks a lot and yes will use these in the future for detecting bugs.

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

In problem F, why the greedy algorithm is correct ?

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

In prob D: In pbds solution can anyone tell me why we need pair in ordered_set? I think it should be ok without pair. Details explanation would be great.

Thanks in advance :)

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

    Since it is a set, if you store multiple same values it will get stored only once and you may lose exact count.

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

For D, Why does Multiset Time out?

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

in Problem F, a very important part of the proof is not mentioned in the editorial. Lemma: for each subtree, if $$$A$$$ and $$$B$$$ are any two sets which cannot be merged, then $$$dA+dB > k$$$. We can prove this by induction on height.

The reason this is an important part of the proof is that when we merge subtrees, we may ask, can't any other possible combination of sets be possible? It is not, because the lemma implies that the way to divide the leaves into sets is unique upto the height of the maximum element in the set ($$$dA$$$ and $$$dB$$$ can't be in the same set).

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Even unoptimized $$$\mathcal{O}(N logN^2)$$$ solution can work comfortably in $$$1400$$$ ms: 160761034