Minimum and Maximum CodeChef


https://www.codechef.com/JULY19B/problems/MMAX


a little bit hint will be great. what is wrong with my I am getting WA

Chef has K chocolates and he wants to distribute them to N people (numbered 1 through N). These people are standing in a line in such a way that for each i (1≤i≤N−1), person i and person i+1 are adjacent.

First, consider some way to distribute chocolates such that for each valid i, the number of chocolates the i-th person would receive from Chef is Ai and the sum S1=∑N−1i=1|Ai−Ai+1| is minimum possible. Of course, each person must receive a non-negative integer number of chocolates.

Then, Chef wants to create a new sequence B1,B2,…,BN by rearranging (permuting) the elements of the sequence A1,A2,…,AN. For each valid i, the number of chocolates the i-th person actually receives from Chef is Bi. Chef wants to distribute the chocolates (choose B1,B2,…,BN by permuting the sequence A and give Bi chocolates to the i-th person for each valid i) in such a way that S2=∑N−1i=1|Bi–Bi+1| is maximum possible. You need to find the maximum value of S2.

It is guaranteed that S2 does not depend on the exact choice of the sequence A1,A2,…,AN, as long as it is a sequence that minimises S1.

 
 
Last edited on
Have a proper look at your 'i' value and try figuring it out yourself :)
yep i fixed it but no luck
.
Last edited on
@abo I dont think in the third case, u give the remaining chocolates to the last person.
It might maximize the sigma( abs(b[i]-b[i+1] ) but that would not satisfy the condition that sigma( abs(a[i]-a[i+1] ) has to be minimum.
@abo I manage to partially solve but none of test case passes in 2ns sub task
Last edited on
@aryastark715 any suggestion for 2nd sub task, my code work only for first sub task.
Last edited on
@aryastark715 Yeah i got my fault . I have updated my explanation now.
Last edited on
@abo its fail in 2nd sub task, still something is missing
Last edited on
I have updated my previous explanation. Have a look and tell what result you get
@natasha1204:
n = 1000
k = 4
you say 6, but answer is 8
@abo can't find your explanation someone reported it
Last edited on
Topic archived. No new replies allowed.