Can you share your approach for reduction game here ?

Can you all kindly please share your approach to Reduction Game Problem.

My approach was :- To keep A_n aside. And work with A from 1 to (n-1). In each iteration, we check for Compare value of min(A_i, A_(n-1)), and if value of this min is > k decrement both, A_(n-1) and A_i and I am continuously sorting array. Here is my code for reference. I know my


[1] will get TLE for highest constraints mentioned in problem, but can you help me optimize my code ?

Please share your approaches.  


  [1]: http://p.ip.fi/LEpo
2 Likes

My initial approach was same :slight_smile: and you can optimise it further by taking decrement value = minimum(|a[i]-k|,|a[n-1] - a[n-2]|) but still not sufficient:(

We can infer below observations:

1. We want to maximise Resultant sum after performing possible decrements

2. We wan directly add elements <= k and remove them… Further decrease remaining elements by k and add k to our answer for each of them(since each would be atleast k)…Now we have new g elements and solve them for k=0 (for easy calculations I did this modifications)

3. Now our Aim is to maximise (Resultant value of g elements)

4. That is we want to maximise ( Greatest element(M1) - Resultant value of remaining (g-1) elements)

5. That is we want to minimise (Resultant value of (g-1) elements)

6. This would have two cases:

I)… If second highest element(M2) is greater than Sum of remaining (g-2) elements then their minimum Resultant value = M2 - Sum of(g-2 elements)

II)… Else this (g-1)elements would cancel out each other and either 0 or 1 would remain depending on their sum is even or odd(since we can decrement only in pairs)

Thus we can solve it with Time Complexity : O(n)

Here is my


[1]



I am not sure it would definitely work but it passed all the random cases I tried...your suggestions are most welcomed...Also if any confusion you are free to discuss :)
  [1]: http://jdoodle.com/a/KvV
5 Likes

Just have a look at this problem. Think how you are tackling the case when you just take moves like (x,y,z) → (x+1,y+1,z) . Now generalise that to this: Given an array of n integers, you choose 2 integers at any time and reduce them by 1 each. Number of moves to make the whole array 0 or print if it is impossible to make. Now you are done. If you are still struggling have a look at my approach for that problem and here I explained my approach. (Its badly formatted though :p)

1 Like

My Python code.

Basically my approach was to sort the given values and find the portion that has values above k. This allowed me to calculate a base sum which is effectively limiting every entry to max k, then work on the excess over k on the relevant large elements.

The basic idea is to try to leave the largest component as big as possible. To do this you want to reduce the second-largest component as much as possible by the smaller components. Then the remainder, if any, gets subtracted off the largest value. Finally if the second-largest component can be reduced completely by the smaller ones, you may still need to take one off the largest bar to ensure that parity is correct, since you always reduce by two in sum.

As you can tell by the above, you can treat the cases of 0, 1 or 2 values above k as special simpler cases.

1 Like

A help would be appreciated :slight_smile:

What output do you get for this case?

4 2
4 4 4 4

@the_extractor :- Ans is 10

Okay. I followed a somewhat similar approach and I also got TLE.

https://discuss.codechef.com/questions/138348/whats-wrong-in-my-approach-reduction-game/138410

This approach seems correct but I’m not able to prove it.

Else this (g-1)elements would cancel out each other and either 0 or 1 would remain

Can you please tell why this is true? Everyone is telling the same logic as yours but no one’s giving a proof of it.

By following this approach you would definitely reach to 0/1 if maximum<=sum(remaining elements):-

while(n>1) { max–; min–; Discard if 0 }

Solve it manually and see the pattern how the elements form pair and reduce each other…For exact proof you may try it by Induction…But I think an algorithm is enough to prove that there is always a way

Yes I saw the pattern by trying various test cases, but this still doesn’t prove it.

But I think an algorithm is enough to prove that there is always a way

No, while we can verify the algorithm by running it on sufficiently large inputs, it would remain a conjecture only, not a proof.

Thanks! understood your approach. Moreover, I stress tested your code on more than 5000 random test cases, and it works fine with all.

Suppose the sum of the first g-2 elements is greater than g-1’st element. Therefore, there have to be at least 2 elements, both smaller than g-1’st element in the g-2 elements. Let the element at g-1 be x and the sum of the first g-2 elements be y. Since there are at least two elements in the first g-2, we can pair some of them up (y-x)/2 times to make the sum of them just smaller than x. Then we can conveniently pair all the remaining elements with the g-1’st element. If in turn, the sum was originally less than x, the optimal way is to pair everything with g-1’st element.

1 Like

@prakhar17252 Thanks a lot! Consider posting this proof as an answer on the solution outlines post: ACMIND18 - ICPC Online Round - Solution Outlines - general - CodeChef Discuss. It’ll help others.

please explain Point no. 4 and 5.