Such questions were very simple and very routine
Let me elaborate on ideas about
For \ (n \) individuals, may wish to assume that everyone has a right to value \ (P_i \)
The \ (P_i \) to support transfer of the transfer rule supports addition, subtraction rule
That is, for the first \ (i \) of us can choose
\(\forall v\in[0,P_i]s.t. v\in Z\Rightarrow P_i -=v,P_{i-1}+=v|P_{i+1}+=v\)
For this kind of problem
I define \ (f_i \) represents \ (i \) individuals were \ (v = f_i \) operations
Then available
\[f_i=f_1-(S_i-w*i)\]
\[S_i=\displaystyle\sum_{j=1}^iP_j\]
\[w=\frac{\displaystyle\sum_{j=1}^nP_j}{n}\]
\ [C_i = S_i-w *
i \] and is equivalent to
\[ans =\displaystyle\sum_{j=1}^nf_j=\displaystyle\sum_{j=1}^n|f_1-C_i|\]
Corresponds to the minimum requirements on the number line such that its distance from the other points and the sum of
\(Code\)
Example P2512 [HAOI2008] Confectionery transfer
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define reg register int
#define isdigit(x) ('0' <= x&&x <= '9')
template<typename T>
inline T Read(T Type)
{
T x = 0,f = 1;
char a = getchar();
while(!isdigit(a)) {if(a == '-') f = -1;a = getchar();}
while(isdigit(a)) {x = (x << 1) + (x << 3) + (a ^ '0');a = getchar();}
return x * f;
}
const int MAXN = 1e6 + 10;
typedef long long ll;
ll a[MAXN],sum[MAXN],b[MAXN],n,fre,ans;
inline ll fabs(ll x) {return (x < 0?-x:x);}
int main()
{
n = Read(1);
for(reg i = 1;i <= n;i++) a[i] = Read(1),fre += a[i];
fre /= n;
for(reg i = 1;i <= n;i++)
{
sum[i] = sum[i - 1] + a[i];
b[i] = i * fre - sum[i];
}
sort(b + 1,b + 1 + n);
ll mina = b[n / 2],ans = 0;
for(reg i = 1;i <= n;i++)
ans += fabs(mina - b[i]);
printf("%lld",ans);
return 0;
}