CHEFTET - Editorial

cheftet
editorial
july16
simple
simple-math

#1

PROBLEM LINK:

Practice
Contest

Author: Dymtro Berezein
Tester: Mugurel Ionut Andreica
Editorialist: Praveen Dhinwa

DIFFICULTY:

simple

PREREQUISITES:

simple math

PROBLEM:

You are given two arrays A and B. For each element B_i, you add B_i to either A_{i - 1} (provided it exists, i.e. i \geq 1), A_i or A_{i + 1} (provided it exists, i.e. if i < n). You have to tell whether you can make all the elements of the array A after all the operations equal or not. If yes, then what is the biggest number that you can achieve?

EXPLANATION:

First thing that you should note that each element of array B is added to some elements of the array A. Also, note that each element is added exactly once.

Due to the above observation, we can notice that sum of all the elements of the array A will be old sum of array A plus sum of array B. Let C denote the final array A we will get after adding elements of B. The sum of all elements of array C will be sum of elements of arrays A and B. Let us denote this sum by S. We want all elements of the array C to be equal, so S should be divisible by n.

Now, each element of the array C will be equal to \frac{S}{n}.

So, we know the final desired value of each element of the array C. It might be possible that this value is not achievable by our operations. If it is possible to achieve this value, then this is the only value that we can get for array C and so by default this will also be biggest value that we can get for each element of C.

Now, for checking whether it is possible to make each element of array equal to S or not. We can use a greedy solution which relies on the fact that for i-th element of array B, we are making changes in array B at quite a few indices which are very near to index i itself.

Let us iterate over array A from left to right and check for each element the set of elements from array B that we can add to get the desired value S.
Let us start from index 1.

  • If A_1 > S, then we can never get C_1 = S, as all the elements of array B are positive.
  • Otherwise, If we have A_1 < S, then we want to add S - A_1 into array A.
    • If B_1 = S - A_1, then we can add B_1 to A_1. Change value of B_1 to 0.
    • Otherwise if B_2 = S - A_1, then we can add B_2 to A_1. Change value of B_2 to 0.
    • If B_1 + B_2 = S - A_1, then we can add B_1 + B_2 to A_1. Change value of B_1, B_2 both equal to zero.
    • Otherwise, we can never make A_1 equal to S.
  • Now, we proceed for A_2. We add B_1 into A_2. Note that if the element B_1 of the original array was added to A_1, then we had already changed it to 0. So, it won’t be added twice. Also notice that you can’t omit to add B_1 now, as if it is not added for A_2, it can’t be added for any element A_3 and onwards. After that want to add B_2 and B_3 and want to make A-2 equal to S. This case is very similar to what we did in the above case. We check the conditions correspondingly and proceed.
  • Now, after the end, we know that we are able to make elements of A equal to S. We should check the condition that each B_i is indeed used. You can do it by checking whether there is some non-zero element B_i in array B.

Note that we are just doing a single pass over the array and greedily deciding the various conditions to check. This will be an \mathcal{O}(n) solution.

Setter’s and Tester’s Solutions:

Tester’s solution
Editorialist’s solution


#2

Can’t access both the solutions.


#3

In this problem can any one help me to understand why i got stuck in the 10th test case…??
here is my solution…
https://www.codechef.com/viewsolution/10759208


#4

How s/n will be the biggest value possible? Not able to understand it, please explain.


#5

why is it that my code is stuck at 7th and 12th test case ? https://www.codechef.com/viewsolution/10849808


#6

Priority is given to i - 1 element to be included, is this the correct order to check:

  1. b[i - 1]
  2. b[i - 1] + b*
  3. b[i - 1] + b[i + 1]
  4. b[i - 1] + b* + b[i + 1]
  5. b*
  6. b[i + 1]
  7. b* + b[i + 1]

#7

can anybody help me why i got stuck in 9th test case?? here is my code:https://www.codechef.com/viewsolution/10873606


#8

i tried it using different method, can anyone check this method and tell why it isn’t satisfying few cases https://www.codechef.com/viewsolution/10874439


#9

Also, the backtracking solution to check all possible states worked :slight_smile:

Code


#10

hi can any one please tell me why my solution is not working for test case 8,9,11 because i have implemented by seeing tester’s solution?
Or can any one please provide me test case at which my program is not working?
Thanks


#11

@prashan_93_y: if remainder of s/n is 0, then it is possible, else it is not possible at all to find such a solution.
If possible, that will be the only biggest value, as you can not make a number greater than average, for each number to be equal.


#12

@the_run: Please add your answer as a comment to @prashan_93_y’s comment. It seems that you mistakenly wrote answer instead of comment.


#13

@aj619 Your code would output “-1” for the following test case wherein the answer is “4”.

1

3

1 1 2

4 1 3

This is because the case “A*+B*+B[i+1]==S” is checked before “A*+B[i-1]+B[i+1]==S” (where S=sum/N). So, in a case where B[i-1]==B*, you would choose to add B* to A* instead of B[i-1] resulting in B[i-1] not being added to any element in array A and also A[i+1], which should have had B* added to it, will be deficit of it.


#14

Fixed now. :slight_smile:


#15

Your order is not correct a*+b[j-1]+b[j+1] should come earlier than a*+b[j]+b[j+1]. Just think why ? If not able to figure out comment :slight_smile: