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

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

1042A - Лавочки

Разбор
Решение

1042B - Витамины

Разбор
Решение

1042C - Произведение массива

Разбор
Решение

1042D - Петя и массив

Разбор
Решение

1042E - Вася и волшебная матрица

Разбор
Решение

1042F - Множества листьев

Разбор
Решение (O(n log n))
Решение (Small to Large, O(n log^2 n))
Решение (O(n))
Разбор задач Codeforces Round 510 (Div. 2)
  • Проголосовать: нравится
  • +57
  • Проголосовать: не нравится

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

so fast :/

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

gone in a flash :o

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

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

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

Loving the quick editorials !

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

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

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

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

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

I'm loving quick editorials. Really helpful.

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

    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. "?

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

      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.

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

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

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

fast as always

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Задача Д https://algotester.com/en/ArchiveProblem/Display/40362 Надеюсь, что это совпадение

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

    Неудивительно, задача же тысячелетний боян. Но, кажется, в див-2 раунде это не страшно.

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

I really hope that no one will fst!

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

Got TLE on testcase 68, coz of using cout,cin DAH! :((((((

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

Got TLE on tc 68 of C , DAAAAAAAH!

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

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

.

.

.

.

.

.

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

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

Can anyone explain me solution to Problem D?

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

    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 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Thanks!

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

      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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

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

        can you pls explain the second parameter in order_of_key?

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

          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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

        0, -1, 1, -2.

        This might clear your doubt

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

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

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

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

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

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 лет назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    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 лет назад, # ^ |
        Проголосовать: нравится +33 Проголосовать: не нравится

      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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 лет назад, # ^ |
      Проголосовать: нравится +4 Проголосовать: не нравится

    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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

    I also solved it using DP.

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

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

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

        see mine, it should be easier to understand.

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

          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 лет назад, # ^ |
            Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

            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 лет назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

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

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

      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?

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

        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 лет назад, # |
Rev. 2   Проголосовать: нравится +36 Проголосовать: не нравится

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 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

        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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

    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: .

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

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

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

      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 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

Failed on tooooo many troubles. Sad.

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

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

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

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 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

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

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

      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.

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

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

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

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?

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

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

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

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

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

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))

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

I think there is a bug in answer checker for problem Array product In the sample test case

4

0 -10 0 0

jury's output is

1 1 2

1 2 3

1 3 4

which is 0

My output is

1 4 1

2 1

1 2 4

which also makes it 0.

So why is it wrong answer?

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

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

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

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.

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

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.

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

In problem F, why the greedy algorithm is correct ?

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

How can we do D(Petya and Array) by using merge sort ?

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

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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

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

For D, Why does Multiset Time out?

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

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).

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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