Write your question here.
Source:-
https://www.codechef.com/JULY19B/problems/MMAX
I did solve this problem but it has been accepted only partially can any one help me to make it correct, so far I know my logic is correct and I am doing some silly mistake.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
long long valueContainer[100000+2];
cin>>n;
cin>>k;
max1 = 0, max2 = 0;
for(iterator=0; iterator < n; iterator++)
{
valueContainer[iterator] = k/n;
}
for(iterator = 0; iterator < k%n; iterator++)
{
valueContainer[iterator]++;
}
sort(valueContainer, valueContainer+n);
long long fs = 0, sc = n - 1;
vector <long long> ValueOne, ValueSec;
while ( fs <= sc ) {
ValueOne.push_back(valueContainer[fs]);
ValueSec.push_back(valueContainer[sc]);
if ( fs != sc ) ValueOne.push_back(valueContainer[sc]), ValueSec.push_back(valueContainer[fs]);
fs++, sc--;
}
for ( long long i = 0; i < n - 1; i++ ) {
max1 += abs(ValueOne[i] - ValueOne[i + 1]), max2 += abs(ValueSec[i] - ValueSec[i + 1]);
}
if(max1 > max2) cout<<max1; else cout<<max2; cout<<"\n";
|