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表示,这就是数学建模,建模完成就开始递推就行了。
还有一个技巧,一堆数都减一个数,这个数是他们的中位数,和最小。
令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; }
相关文章推荐
- [UVa 11300]Spreading the Wealth
- UVA 11300 Spreading the Wealth
- UVA 11300 Spreading the Wealth
- UVa11300 Spreading the Wealth
- UVa 11300 Spreading the Wealth
- UVa 11300 Spreading the Wealth 数学题
- UVA - 11300 Spreading the Wealth (构造方程组 转几何)
- UVA 11300 Spreading the Wealth(思维题)
- UVa 11300 - Spreading the Wealth
- UVA11300:Spreading the Wealth
- UVA-11300 - Spreading the Wealth
- UVA 题目11300 - Spreading the Wealth
- UVa11300 - Spreading the Wealth(方程求解+中位数)
- UVA11300:Spreading the Wealth
- UVA - 11300 Spreading the Wealth (公式)
- 数学/思维 UVA 11300 Spreading the Wealth
- uva 11300 Spreading the Wealth 中位数应用
- UVa11300 Spreading the Wealth(数学问题)
- 【uva 11300】 Spreading the Wealth
- UVA 11300 - Spreading the Wealth(中位数)