您的位置:首页 > 其它

UVA 11300 Spreading the Wealth

2017-08-17 09:45 363 查看
首先对该题进行数学建模,

令x2为2号给了一号多少金币

2号最终剩余M个金币,M是最后每个人应该剩下的金币

M=a[2]-x2+x3;

同理M=a[3]-x3+x4;

M=a[1]+x2-x1;

x2=M-a[1]+x1;

x3也可以用x1表示,这就是数学建模,建模完成就开始递推就行了。

还有一个技巧,一堆数都减一个数,这个数是他们的中位数,和最小。

#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
//ios::sync_with_stdio(false);
const int maxn=1000009;
long long a[maxn],c[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
long long tot=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
tot+=a[i];
}
long long M=tot/n;
c[0]=0;
for(int i=1;i<n;i++)c[i]=c[i-1]+a[i]-M;
sort(c,c+n);
long long x1=c[n/2];
long long ans=0;
for(int i=0;i<n;i++)ans+=abs(x1-c[i]);
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: