SPatrik's blog

By SPatrik, history, 3 months ago, In English,
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

 
 
 
 
  • Vote: I like it
  • +37
  • Vote: I do not like it

»
3 months ago, # |
  Vote: I like it +37 Vote: I do not like it

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?

  • »
    »
    3 months ago, # ^ |
    Rev. 3   Vote: I like it +20 Vote: I do not like it

    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.

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

    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.

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

      Proof by contradiction. Nice.

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

      This definately proves that if the largest element is greater than the sum of all other elements — Then all elements can never be made 0. Cool...!

      But if largest element is less than sum of all other elements ; how can you guarantee that it is always possible to make all elements = 0...? Could you explain this please...

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

      Great!

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

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

    5

    1000000000 1000000000 1000000000 1000000000 1000000000

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

      got it

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

        Can you please explain this testcase I am still not able to understand.

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

      This is like 2 2 2 2 2 We make the following changes

      1 1 2 2 2

      1 1 1 1 2

      0 0 1 0 1

      0 0 0 0 0

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

      5 100 100 100 100 100

      50 50 100 100 100 50 0 50 100 100 50 0 0 50 100 50 0 0 0 50 0 0 0 0 0

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

    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!

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

      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.

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

      Thanks for your reply^-^

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

    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!

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

      One of the best explanations! However, this does prove that these are the necessary conditions, but how do we know these are the only sufficient conditions?

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

        After this constraint is applied, the question simply reduces to : Design array with given even sum and maximum element less than equal to sum/2, by incrementing any number, which is trivial to see I think.

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

    I approached to this question from backward. I considered case when we can’t reduce it further. It should have zeros and some even number, e.g. 0 0 2. If we take one step back, there were two possibilities to reach final point (either 1 1 2 or 0 1 3). 1 1 2 can be reduced to zeros, so we left with 0 1 3. It becomes obvious that we can’t reduce ‘3’ to zero, because sum of other numbers is less than 3.

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

    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)

  • »
    »
    3 months ago, # ^ |
    Rev. 3   Vote: I like it +7 Vote: I do not like it

    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.

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

      How can you explain this with [3, 5, 6]

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

        $$$X = 6$$$, $$$remSum = 8$$$ (both numbers are even and remSum is greater than X by an even amount $$$8-6=2$$$

        Subtract 1 from 3 and 5, now remSum = 6 and X = 6.

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

      Really good explanation. Thanks!

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

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

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

    I am not sure, but try to swap rows "cin.tie(0)" and "iostream..."

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

      It doesn't work. :(

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

      He shouldn't swap them. ios_base::sync_with_stdio(false) or equivalent resets cin.ties state. So you should always have them in that order.

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

    in using colum[where[j]+k], where[j]+k is an invalid index. it is accessing an index beyond size of the vector.

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

      I fixed it but it still doesn't work. I don't know why. And it doesn't work in test case 1 in CF but in works when I try it in Xcode... https://codeforces.com/contest/1201/submission/58320302

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

        ll where[m] is not initialized properly. You forgot to assign values from 0 to colum[0]. So it causes an index overflow when you try to refer to colum[where[j] + k]

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

          yeah, you are right! thank you!

»
3 months ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

Bonus: solve problem C with linear time complexity.

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

    Was binary search actually necessary? I thought that simply sorting and maintaining how many elements would require updating to increase the median for each i between n / 2 and n would suffice.

    For instance, if the initial median were some 'x' and the successor of 'x' were to be 'y' in the given array, it would require 'y' — 'x' updates to make 'x' = 'y'. Now, as any further increments to 'x' would yield 'y' as the median, it mandates updating both terms (equal to 'y') to their succeeding value. This can be maintained using a 'termsToUpdate' counter.

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

      Sorting is not linear.

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

        Oops, my bad. Though, I guess a similar technique could be used with quickselect. Not too sure about it.

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

          I guess no because we not only want median but also number greater than median in a sorted order so I think sorting has to be done.

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

    Note that the array has to be sorted.

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

    Sorry, I didn’t notice that the array in this problem is not sorted. It was hard to notice it for me because I have the same problem with sorted array in Polygon. Of course I meant linear solution with sorted array. Sorry for misunderstanding.

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

      Well I guess sorting of integers $$$<=10^9$$$ with $$$N$$$ equal to $$$1e5$$$ might be considered as linear [almost].. just 2 counting sorts

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

    58273442 Is that what you meant?

  • »
    »
    3 months ago, # ^ |
      Vote: I like it -7 Vote: I do not like it

    Well, if we ignore the complexity of sorting the array then we can just iterate from i=n/2 to i = n-1 and keep making all elements between n/2 to i. equal to A[i]. We break the loop if k goes negative. Here is code. https://codeforces.com/contest/1201/submission/58290181

  • »
    »
    3 months ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    why I am getting WA on test 25 submission link: div2/a

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

    Igoring sorting, my solution is pretty fast.58914655

»
3 months ago, # |
  Vote: I like it +22 Vote: I do not like it

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.

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    include <bits/stdc++.h>

    using namespace std; int a[200005]; int main() {

    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    
    sort(a,a+n);
    
    int med=a[n/2];
    long long mid;
    long long l=med;
    long long r=2000000009;
    while(l<=r)
    {
    
       // mid=(l+r)/2;
        mid = l + (r-l)/2;
        //cout<<mid<<endl;
        long long op=0;
    
        for(long long i=n/2;i<n;i++)
        {
            if(mid>a[i])
            {
                op+=(mid-a[i]);
            }
        }
    
        if(op==k)
        break;
    
        else if(op > k)
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    
    }
    cout<<mid;

    return 0; }

    Could you explain why Binary search is giving wrong ans :)

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

can someone please explain D solution in english

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

    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.

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

      i am not able to understand the four paths which you are talking about,can you please elaborate a little.what i am thinking is left -> leftand left -> right and right -> right and right -> left.am i thinking right

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

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.

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

What's in the 6th testcase of Problem C?If anyone figured it out?I am unable to find the error in my code which is failing at this testcase. link to my code

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

    Never mind.Thanks

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

      Can you share how you solved it? I am stuck at test 6 as well.

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

        Check on cases when the next candidate for the median does not lies in the array.You can also see the difference between my AC code and code failing at TC-6. Use this case- Input 13 11 3 3 1 2 3 3 3 5 6 9 10 3 8 Output: 7

»
3 months ago, # |
  Vote: I like it +1 Vote: I do not like it

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

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

    So to get started, if we need to get the median of an array, we first sort that array and take the middle element (suppose the array length is always odd).

    Consider a median in an sorted array. It is guaranteed that all elements on the left of the median are less than or equal to the median, and all elements on the right of the median are greater than or equal to the median.

    The idea is to first preserve the element that is the median of the array, and by "preserve," I mean that the element would stay at its position and would not move anywhere. The only thing happens is that the element (which is also the median) gets increased.

    The only (valid) operation here that can be performed is +1. For the median to increase in value, while still stays where it initially is (in an sorted array), the median itself and all elements to its right side have to increase accordingly.

    By "accordingly," consider this sorted array:

    1 2 2 3 5 7 7

    The median here is $$$3$$$. If I am to apply +1 two times on the median, it will be $$$5$$$ and it would still be the median. However, if I apply +1 three times on the median, it will be $$$6$$$ and will not be the median anymore, so to preserve its position, I have to increase the element $$$5$$$ after the median $$$3$$$ as well, which means one more +1 operation. (If we don't keep the current median's position, we will waste some +1 operations.)

    The solution in the editorial uses binary search. Suppose I am to achieve a median of $$$x$$$, then a certain number of +1 operations will be applied to match everything I have mentioned above. A smaller $$$x$$$ requires fewer +1 operations, while a bigger $$$x$$$ requires more. Binary search is applied to find out the maximal $$$x$$$ that we can achieve, while still satisfy the constraint for $$$k$$$ (the number of +1 operations allowed).

    $$$\sum_{i = \frac{n+1}{2}}^{n}{max(0, x - b_i)}$$$ could be interpreted in this way: Go from the median position to $$$n$$$, if $$$x$$$ is greater than $$$b_i$$$ then we add $$$x - b_i$$$ to the total number of +1 operations being used(, otherwise if $$$x$$$ is not greater then we just add zero, which means no any +1 will be used on $$$b_i$$$ (because +1 operation is there to increase an element's value and in this case we try to raise elements up to $$$x$$$ using +1 operations so if $$$b_i$$$ is already greater than $$$x$$$ then we don't even need any +1 operations)).

    A few more observations on the sorted array, you may figure out a linear solution (on a sorted array) to find out the answer.

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

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?

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

    Closest safe columns right to the leftest treasure, left to the leftest treasure and same for rightest treasure.

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

      So cant we get the minimum of the right and left safe column?

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

        2 23 3 3
        1 19
        2 22
        2 1
        1 16 23
        correct answer:
        45

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

          So how we can see which one is better? Do we need to check that if we go left then at the next we have to go there? And if its then it will effect on other rows chooses ? So we need dp?

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

            of course we need dp, we must know answer for leftmost-left, leftmost-right, rightmost-left and rightmost-right. after we use that 4 numbers to calculate same thing for next line

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

          Why is the right answer $$$45$$$ and not $$$44$$$? One possible path of weight $$$44$$$ is $$$(1,1)\ ->\ (1,19)\ ->\ (1,23)\ ->\ (2,23)\ ->\ (2,22)\ ->\ (2,2)$$$.

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

            sorry, I thought that third treasure was at 2 1

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

The third problem is pretty good!

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

Can anyone please explain me the solution of 3rd problem, i am unable to understand the editorial.

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

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.

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

.

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

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

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

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

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

can someone explain problem C editorial better please?

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

    Yeah, specifically the method of applying binary search in this condition.How do you realise that binary search must be done?

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

    Take a look at my comment here.

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

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

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

can anyone explain me how problem one works?

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

    There are $$$m$$$ answers for each of $$$n$$$ students in class. To hope for the maximum score possible, for each question, ask every student in the class about which choice he/she gave on the exam, and take into account the choice that most students chose for that question. In other words, suppose the majority of students in the class chose B for question 1, then let's say B would be the correct answer. Similarly for all other questions.

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

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

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

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://codeforces.com/contest/1201/submission/58301289

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

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

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

    consider 5 2 2 2 2 2

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

    And why the condition 2: The biggest element have to be less or equal than the sum of all the other elements Have to be satisfied ?

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it
n,k=map(lambda n:int(n),input().split(' '))
a=list(map(lambda n:int(n),input().split(' ')))
a.sort()
m=(n-1)//2
add=0
i=m
while(i<len(a)-1):
    add+=a[i]
    diff=i+1-m
    if a[i+1]*diff-add>k:
        break
    k-=a[i+1]*diff-add
    i=i+1
print(a[i]+k//(i-m+1))

This is my solution to div 2 1201C problem.It is showing wrong answer on test6.Can anyone tell me where is the bug.It seems pretty confusing to me.

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

Implementation of D seems hard for me. Can someone help me to implement or share an easy implementation if they feel?

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

please somebody help me,i cannot understand solution of C problem

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

How is the problem B different from the array partition problem, where you have to divide the array into two equal sum subsets. If yes, then are the two given condition sufficient to solve that as well?

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

In Div2 B, for n=4 and arr={2,4,4,8}. It can't be reduced to zero. But from above idea it is possible.How?

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

    @var_i arr={2,4,4,8} can be reduced to zero This is how it can be done 2 4 2 6, 2 2 2 4, 2 2 0 2, 2 1 0 1, 1 1 0 0, 0 0 0 0

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

What's the problem with this submission https://codeforces.com/contest/1201/submission/59435091 and the approach? I am iterating on the sorted array from n/2 and storing the increase required for all numbers to become equal till i.

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

For Problem C

include <bits/stdc++.h> using namespace std; int a[200005]; int main() {

int n,k; cin>>n>>k; for(int i=0;i<n;i++) { cin>>a[i]; }

sort(a,a+n);

int med=a[n/2]; long long mid; long long l=med; long long r=2000000009; while(l<=r) {

// mid=(l+r)/2; mid = l + (r-l)/2; //cout<<mid<<endl; long long op=0;

for(long long i=n/2;i<n;i++)
{
    if(mid>a[i])
    {
        op+=(mid-a[i]);
    }
}

if(op==k)
break;

else if(op > k)
{
    r=mid-1;
}
else
{
    l=mid+1;
}

} cout<<mid; return 0; }

Could you explain why Binary search is giving wrong ans :)

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

In Maximum Median, 2nd example, shouldn't the correct answer be 4?
We start with [1, 1, 1, 1, 2]
Then [1, 1, 2, 2, 2] (2 increases)
Then [1, 1, 3, 3, 3] (5 increases)