(洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币

时间:2023-03-09 05:01:19
(洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币

bzoj1045

洛谷P4016

洛谷P2512

bzoj3293

洛谷P3156

题解:https://www.luogu.org/blog/LittleRewriter/solution-p2512

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
int n;ll c;
ll a[],b[];
ll abs1(ll x){return x>=?x:-x;}
ll an;
int main()
{
int i;ll t;
while(scanf("%d",&n)==)
{
c=;an=;
for(i=;i<=n;++i)
{
scanf("%lld",&a[i]);
c+=a[i];
}
c/=n;
for(i=;i<=n;++i)
{
a[i]-=c;
b[i]=b[i-]+a[i];
}
int p=(n+)/;
nth_element(b+,b+p,b+n+);t=b[p];
for(i=;i<=n;++i)
an+=abs1(t-b[i]);
printf("%lld\n",an);
}
return ;
}