Minimum and Maximum CodeChef

Jul 6, 2019 at 12:13pm

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 Jul 6, 2019 at 4:15pm
Jul 6, 2019 at 2:23pm
Have a proper look at your 'i' value and try figuring it out yourself :)
Jul 6, 2019 at 2:53pm
yep i fixed it but no luck
Jul 6, 2019 at 4:40pm
.
Last edited on Jul 7, 2019 at 6:01am
Jul 6, 2019 at 5:41pm
@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.
Jul 6, 2019 at 6:11pm
@abo I manage to partially solve but none of test case passes in 2ns sub task
Last edited on Jul 6, 2019 at 6:17pm
Jul 6, 2019 at 6:14pm
@aryastark715 any suggestion for 2nd sub task, my code work only for first sub task.
Last edited on Jul 6, 2019 at 6:16pm
Jul 6, 2019 at 6:27pm
@aryastark715 Yeah i got my fault . I have updated my explanation now.
Last edited on Jul 6, 2019 at 6:36pm
Jul 6, 2019 at 6:40pm
@abo its fail in 2nd sub task, still something is missing
Last edited on Jul 6, 2019 at 6:41pm
Jul 6, 2019 at 7:15pm
I have updated my previous explanation. Have a look and tell what result you get
Jul 7, 2019 at 1:37am
@natasha1204:
n = 1000
k = 4
you say 6, but answer is 8
Jul 7, 2019 at 9:10am
@abo can't find your explanation someone reported it
Last edited on Jul 7, 2019 at 9:10am
Topic archived. No new replies allowed.