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

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

C++ code: 58307249

Tutorial is loading...

C++ code: 58307265

Tutorial is loading...

C++ code: 58307281

Tutorial is loading...

C++ code: 58307302

Tutorial is loading...

C++ code: 58307320

$$$O(n^4)$$$ complexity solution for problem E1 by KAN: 58306483

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

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

Can anyone give a mathematical proof for Div 2 B, second point? It seems correct intuitively, and not able to come up with counter cases. How does everyone discover this fact?

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

    At each turn, take the maximum element and the minimum element, and reduce each by 1. At this point the second point still holds true.

    Consider the case when the maximum element got reduced by 1, and it became not the maximum. Then the sorted array looks like this:

    $$$....., x, x - 1.$$$

    Since the total sum is still even, there is another positive element ($$$\ge 1$$$), apart from these two. Then $$$x \le (x - 1) +$$$ "some elements, at least one of which is 1". So the second point still holds, and we continue with x as the maximum element.

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

    I'll have a crack at a possible explanation.

    The problem statement instructs us to pick 2 distinct indices and reduce each element by 1. Suppose the point in consideration "The biggest element has to be less or equal than the sum of all the other elements." were to be false. Let this biggest element be at index i. For all other indices j, if we pick (i, j) as our pair and reduce their values by 1, we should be able to see that it won't be possible to reduce the largest element to 0.

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

    How this test case correct answer is YES, for problem 2? Can anyone explain that?

    5

    1000000000 1000000000 1000000000 1000000000 1000000000

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

    Here is another way to think about it.

    Consider 2 cases:

    • Base case: the largest element is equal to the sum of all the others:

    Let's say that each element of the array represents the number of marbles of a given color. We can take 2 bowls. On the 1st, we put all the marbles of the largest number, while on the 2nd we put all the remaining ones. On each operation, we can take one marble from both of them. As they contain the same number of marbles, they will become empty at the same time and we are finished!

    • Second case: the sum is bigger than the largest element:

    We will try to reduce this to the above case! Again, we fill the bowls with the same way. The difference is that now the 2nd bowl has more marbles. To solve this, we can begin picking pairs from it until they are equal! Note that, as the sum is an even number, the parity of the number of marbles on each bowl is the same (ie. both odd or both even). That guarantees us that we will 100% reach a point that they are equal!

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

      But why can't we get stuck while picking pairs from second bowl in the second case? This needs a bit of explanation.

      I see it differently. If one element is exactly half of all, we can easily pair it with others. But if all are less than this half, then we can pick any pair (it exists, since there are at least 2 colors, since if there were just 1 color, it would have all marbles and that is not less than half). Picking any pair reduces total count by 2 (or half by 1). As soon as this "half" gets equal to any of the values, we switch to case 1. Since it gets decremented by 1, it will be equal to some of the lower values without skipping it, sooner or later.

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

    First you must realize that finally, in last step of reduction, the array will be of form: 1 1 0 0 0 0...... So if we backtrack from here to generate all possible arrays, at each step you increment the whole sum of array by 2, but for any element, you can only increase it by 1 in that step.So even if a single element is focused on every time, max(arr[i])<=sum/2. Hope it helps!

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

    One more proof:

    Let's make an optimal selection of pairs — we would have P pairs and U unassigned.

    1). If U is empty — we can zero array

    2). If U is odd — sum of an array was odd and the answer is NO

    3.1). If U is even and has more than 2 elements — all U must be coming from a single A[i] because our selection is optimal.

    3.2). If there is a pair in P (a, b) and both a and b are not from i — we can reassign 2 elements from U and a, b and make U lower. Which is impossible because our selection is optimal. Thus, all pairs in P has one element from A[i] and other element from somewhere else. So it means, A[i] = U + P, and sum(A) - A[i] = P. A[i] must be biggest element and it is bigger than half of sum(A)

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

    Explaining the case where the maximum element is less than the sum of the remaining elements:

    Let's call the maximum element X, the sum of the remaining elements remSum and X+remSum is the total sum.

    Since X+remSum must be even, then X and remSum must both be even or odd, then for remSum to be greater than X it has to be greater by an even amount remSum = X+2*k, otherwise the total sum won't be even.

    Consider the case where X = 5 and remSum = 7 Notice: remSum is greater than X by 2 (an even amount)

    1, 1, 2, 3, 5 where X = 5 and remSum = 1 + 1 + 2 + 3

    decrease remSum by 2 using any pair of elements from remSum, e.g. the pair at indices 0 , 3

    the resulting elements are 0, 1, 2, 2, 5

    Now X = 5 and remSum = 5, subtracting X from all elements of remSum will then result in an array of all 0's.

    Using this fact, we are sure that for any case where remSum > X and remSum+X is an even number, we can always remove the extra even amount 2*k from remSum and reach a feasible state where the sum of the remaining elements is equal to the maximum element.

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

      Really good explanation. Thanks!

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

      Hey. Can u pls show its working on 1,3,4,4

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

      Really great explanation....

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

      Tell me how do you guys think in such ways...I mean how its easy to prove the conjecture but tell me how do you approach the problem...

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

      Thanks for this great explanation!

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

      Great Explanation

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

      This proof isn't strong enough; I still can't be sure why we can necessarily reduce the residual sum by 2*k.

      I propose the following model, which seems sound to me, but which I'm currently having trouble proving(only for a1 <= sum / 2 case, because others are simple):

      If we want to zero the sequence — it's mean that we want to perform exactly (sum / 2) operations. (Because each operation decrease the total sum by 2).

      (let $$$sum = a_{1} + a_{2} + ... + a_{n}$$$)

      So we need to prove:

      "The sequence can be zeroed, if we can perform (sum / 2) operations on a sequence {a1, a2, ..., an}"

      Let sort all the values in non-ascending order.

      And let S = remained elements (without max)

      Then we have 4 contidions.

      1) $$$a_{1} \gt = a_{2} \gt = a_{3} \gt = ... \gt = a_{n}$$$

      2) $$$S = a_{2} + a_{3} + ... + a_{n}$$$

      3) (a1 + S) % 2 = 0 (because if not — it's impossible to zero)

      4) $$$(a_{1} \lt = S)$$$

      And we need to prove that our sequence can be zeroed.

      1) Obviously, that if a1 == S, then we can just always take a1 in each pair, and any other element with it, then a1 will be zeroed as soon as S will be zeroed.

      2) But let $$$a_{1} \lt S$$$.

      This situation is much harder.

      But, our target is to make the sequence satisfying the situation #1 $$$(a_{1} == S)$$$.

      We know, that (a1 + S) % 2 = 0.

      Then a1 % 2 = S % 2 (both are even or both are odd, common number theory, you can prove it if you're not sure).

      Then difference (S — a1) will be also even as (S + a1) (odd — odd = even; even — even = even).

      And if we want to make S = a1, then we need to somehow subtract (S — a1) from S.

      We need exactly (S — a1) / 2 operations to get this.

      So, we can reformulate our statement that we want to prove:

      "The sequence can be zeroed if we can perform (S — a1) / 2 operations on a sequence {a2, a3, ..., an}"

      We already see, that we have some recursion, so the first statement will be proved if the current statement is proved.

      But I'm already spend a lot of time on this math problem and I need to rest. Sorry. If you have any thoughts, please let me know.

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

        "I still can't be sure why we can necessarily reduce the residual sum by 2*k"

        Think about it step by step, while remSum > maxi, pick any 2 numbers from remSum and subtract each by 1.

        The idea is that regardless of which numbers do you pick, if remSum > maxi, there must be at least 2 positive numbers in remSum, because if there is only one number, then this means that this one number is bigger than maxi, which is a contradiction.

        so we can keep doing this until remSum equals maxi. Hope this helps

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

    he made the question and god knows the proof ..

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

    I am 18 months late, if anyone still has a doubt...here you go

    Let the elements of the array be [a,b,c,d] where d is the largest element

    Let us assume d is smaller than or equal to a+b+c (sum of all other elements)

    d = a+b+c — delta

    we know that d+a+b+c is even, 2(a+b+c) — delta is even and hence delta must be even.

    Now, we can keep picking any 2 elements from [a,b,c] and reduce them delta/2 times so that d becomes equal to a+b+c...

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

    Let the array be sorted : a1,a2,a3,a4,...an-1,an

    if an = sum of remaining elements , it is quite evident

    for an < (sum of remaining elements) :

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

Can someone help me? I cant find what's wrong with my code. https://mirror.codeforces.com/contest/1201/submission/58307080

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

Bonus: solve problem C with linear time complexity.

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

Proof for B:

Claim 1: the initial sum must be even

The operation does not change the parity of the sum. Since the sum of an array of zeros is even, the initial sum must also be even.

Claim 2: assuming the initial sum is even, then it is necessary and sufficient that there is no element in the initial array that is larger than half of the initial sum

To prove necessity, consider an initial array with an element that is larger than half of the initial sum. Thus, this element is also larger than the sum of the other elements. Separate this from the other elements. Notice that, each operation either subtracts 1 from the element and subtracts 1 from the others, or subtracts 2 from the others. Thus, we can only subtract as much from the element as the sum of the others. Since the element is larger than the sum of the others, it will not reach zero.

To prove sufficiency, we notice the following properties of an array of zeros: its total sum is zero and its largest element is not larger than its total sum. Since each operation decreases the total sum, we will eventually reach zero. Call an array good if it has no element larger than half of its total sum. Thus, we only have to prove that we can always perform an operation on a good array with nonzero sum such that the result array is also good. Consider a good array with nonzero sum. We claim that subtracting 1 from the two largest elements will result in a good array. Note that there is at least one nonzero element since the sum is nonzero. And there are at least two since the array is good. Thus, it is always possible to subtract from two elements. If subtracting from the two largest elements doesn't result in a good array, then there is an element distinct from the two largest elements that is larger than $$$\frac{s - 2}{2}$$$, where $$$s$$$ is the sum of the array before subtracting from the two largest elements. Since the two largest elements are at least as large as this element, the total sum before subtraction is at least $$$3 \cdot \frac{s}{2}$$$ but at most $$$s$$$. However, this leads to the inequality $$$s \leq 0$$$. Since the sum can never be less than zero, $$$s = 0$$$. However, we assumed that the sum is nonzero. This is clearly a contradiction. Thus, subtracting from the two largest elements of a good array will always result in a good array.

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

can someone please explain D solution in english

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

    Every time you reach a row that has treasure, you have to get all treasure before you go to the next row, so you will finally stop at the leftmost or the rightmost treasure. It's the same for the next now that has treasure. So you can just calculate the cost of 4 paths: L1->L2 / L1->R2 / R1->L2 / R1->R2 , and save the minimum of L2 and R2 for the next row, continue to do the same thing.

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

A minor issue for time complexity in problem C. The sorting is already O(nlogn). Or is it supposed to ignore such trivial operations in complexity analysis? Besides, a linear implementation to solve the problem after sorting is here.

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

Can someone please explain C in detail, I am not able to understand it.

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

How it's 4 possibly for every row in problem D? 1- we can go to rightmost and then left most and then to the closet safe column to the leftmost one treasure. 2- we can go left and then right and then to the closet safe column. (reverse order of 1)

what possibilities im not considering?

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

In problem D if we assume that each row has atlest one treasure then does this greedy computes optimal answer?

Move in zig-zag fashion i.e. In first row collect treasures in order from left to right. In second row from right to left, in third from left to right and so on.

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

.

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

I have solved in this way. (div2/c) can anyone tell in what catergory my solution goes? (Binary Search, greedy, math)? submission: link

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

Could someone tell me what's wrong with my solution for D https://mirror.codeforces.com/contest/1201/submission/58341150

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

can someone explain problem C editorial better please?

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

I'm curious if anyone has Greedy solution for Treasure Hunting (D) (Greedy tag)

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

Isn't time complexity of editorial's solution for problem C nlogn+log(1e9)? Someone please fix me if I'm wrong about that.

My solution for problem C. Complexity: nlogn + n/2 58305249

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

Hey, I came up with an O(nlogn) + linear time complexity for Div2 C, kindly correct me if I am wrong or a similar solution has been posted!

  1. Sort the array (nlogn)
  2. Start from the middle (A[n/2])
  3. Find the current max
  4. Change required += (curr_max — old_max) * (len-1)
  5. When change exceeds k, quit

answer is (max + ((k-change)/len)) https://mirror.codeforces.com/contest/1201/submission/58301289

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

Can anyone explain me how the answer for the "B. Zero Arrays " for the 5th test case " 5 1000000000 1000000000 1000000000 1000000000 1000000000 " The answer is "YES" How ??

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

DIV2 D is very similar to this 1066F - Yet another 2D Walking

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

I had a doubt about the B (Zero array) question. I understand that given conditions are necessary, but how do I prove that they are sufficient?

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

I think for a case of 1201B 1 2 3 3 4 5 6 though it is satisfying condition 1 and 2 , it seems impossible to get all 0. Can anyone help me?