【思维题】uva11300Spreading the Wealth
来源:互联网 发布:最优化理论 课程 推荐 编辑:程序博客网 时间:2024/06/15 00:44
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2275
题目描述:环形排列的n
样例输入:
3
100
100
100
4
1
2
5
4
样例输出:
0
4
方法一:
进行数学分析:
设最终每个人有m个金币。第i个人给左边的人(第i-1个人)
那么得到n-1个方程:
……
理论上来说应该还有一个方程式:
现在要求的就是
求中位数的算法可以用sort计算得到,也可以用快速选择算法(建议去看一看《算法导论》,上面讲的十分清楚)。
蒟蒻不才,想了半天还是看了题解才做出来的……
#include <iostream>#include <cstdio>#include <algorithm>#define abs(a) ((a)>0?(a):(-(a)))#define MAXN 1000005#define LL long long intusing namespace std;int n ;LL money[MAXN] ,num[MAXN] ,ans ,result ,sum ;int main(){ while(~scanf("%d",&n)) { sum=ans=0; for(int i=1;i<=n;++i) { scanf("%lld",&num[i]); sum+=num[i]; } sum/=n; for(int i=1;i<n;++i) money[i]=money[i-1]+num[i]-sum; sort(money,money+n); result=money[n/2]; for(int i=0;i<n;++i) ans+=abs(result-money[i]); printf("%lld\n",ans); } return 0;}
方法二:
令
对其中一组解,每一个
实现:
1.找到任意一组可行解。
2.找到中位数
3.计算
- 【思维题】uva11300Spreading 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
- 11300 - Spreading the Wealth
- 11300Spreading the Wealth
- UVA11300:Spreading the Wealth
- 11300 - Spreading the Wealth
- UVA11300 Spreading the Wealth
- uva11300 Spreading the Wealth
- UVA_11300Spreading the Wealth
- Spreading the Wealth
- Spreading the Wealth
- 11300 - Spreading the Wealth
- Uva11300 - Spreading the Wealth
- UVa11300 - Spreading the Wealth
- 7-29
- 备注
- struts-ajax实现局部刷新分页
- EularProject 36:2进制和10进制回文数
- CSS基础-7CSS样式-文本
- 【思维题】uva11300Spreading the Wealth
- Java使用Dom解析xml学习记录
- 编程语言的学习生涯 连载一北京校区53班赵凯
- 初学java,写了一个mysql类(增删改查)
- dropwizard metrics - 基本使用介绍
- JAVA中的接口和抽象类
- IOS 中 运行时 简单应用 扩展类添加属性 遍历成员变量
- mtk android 4.4 audio framework 代码分析(未完成)
- Android 平台上的 ARP 和 ARP欺骗