uva11300 - Spreading the Wealth(分金币)
来源:互联网 发布:如何用c语言编游戏 编辑:程序博客网 时间:2024/05/21 17:39
代数分析::::
先算出平均数ave然后我们开始分析,,,,
每个人都只有给出和给进两部分,如果用数字表示则是一负一正
1号座位的人只与4号和2号有联系,则有x1表示1给4的,同理x2表示2给1的。
如果x1<0则表示4给1了-x1个金币。。
1: A1-x1+x2=ave -> x2 = ave-A1+x1 = x1-C1(C为常数)
2: A2-x2+x3=ave -> x3 = ave-A2+x2 = ave-A2+x1-C1=x1-C2
3: A3-x3+x4=ave -> x4 = ave-A3+x3 = ave-A3+x1-C2=x1-C3
.....
n: An-xn+x1=ave -> xn = ave-An+x1 = ave-An+x(n-1)=x1-Cn
我们的目的要求|x1|+|x1-C1|+|x1-C2|+...|xn-Cn|的最小值,
我们可以把上式看作要求点x1使得x1到0,C1,C2....Cn的距离最短。
然后求出最小值就行了。这就是代数分析。
很实用的能把题目难度降低的思路之一。。。。
代码如下:
#include <cstdio>#include <algorithm>using namespace std;#define M 1000010long long a[M], c[M];int main (){ long long n, sum, ave; while(scanf("%lld",&n)==1) { sum = 0; for(int i = 1; i <= n; i++) {scanf("%lld",&a[i]); sum+=a[i];} ave = sum/n; c[0] = 1; for(int i = 1; i <= n; i++) c[i] = c[i-1]+a[i]-ave; sort(c,c+n); long long x1 = c[n/2], ans = 0; for(int i = 0; i < n; i++) ans+=abs(x1-c[i]); printf("%lld\n",ans); } return 0;}
- uva11300 - Spreading the Wealth(分金币)
- UVa11300 - Spreading the Wealth (分金币)
- Spreading the Wealth (分金币)
- UVA11300:Spreading the Wealth
- UVA11300 Spreading the Wealth
- uva11300 Spreading the Wealth
- Uva11300 - Spreading the Wealth
- UVa11300 - Spreading the Wealth
- Uva11300 - Spreading the Wealth
- uva11300 Spreading the Wealth
- UVA11300:Spreading the Wealth
- UVA11300: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 分金币 (中位数)
- 中位数-分金币(Spreading the Wealth, UVa 11300)
- Ubuntu下MySql ERROR 1045错误 ---通过修改密码可以解决问题
- Ubuntu 下Qt SDK的下载及安装
- cocos2d-x 场景的切换
- The used SELECT statements have a different number of columns
- udp客户端程序
- uva11300 - Spreading the Wealth(分金币)
- ubuntu gedit 中文显示乱码解决方法
- cocos2d-x 图片文字显示
- Android开机启动流程
- cocos2d-x Touch 移动精灵
- cocos2d-x CCAction:动作
- tcp 服务器server
- cocos2d-x 粒子系统
- 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚