uva - 11300 - Spreading the Wealth(数学推导,中位数)
来源:互联网 发布:刷搜狗手机点击优化 编辑:程序博客网 时间:2024/06/11 11:33
题意:刘汝佳大大的例题,分金子。
方法:首先推导出公式例如A1 - x1 + x2 = M,然后根据公式把每个人金币的变化表示出来,例如x2 = x1 - C,C = A1 - M(M是最后每个人应该有的金币数,也就是平均数)。 x3 = x1 - C,不过C变化成A1 + A2 - 2M。依次推出C就好。然后就变成了求x1到各个C的距离,用中位数解决。
注意:因为看的例题,所以用long long。但是输入金币的时候没感觉要用 long long。。【后来测试A数组可以用int,C数组也可以,没测试tot,估计是不行】。
#include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <algorithm> #include <cmath> using namespace std;#define MAX 1000000+10long long A[MAX], C[MAX], tot, aver, n, sum;void Input(){int i = 0;for (i = 0; i < n; i++){cin >> A[i];tot += A[i];}}void Creat_C(){int i = 0;C[0] = 0;for (i = 1; i < n; i++)C[i] = C[i-1] + A[i] - aver;}void Calculate(){int i = 0;long long mid = C[n/2];for (i = 0; i < n; i++)sum += abs(C[i] - mid);}int main() { #ifdef Local freopen("a.in", "r", stdin); #endifint i = 0, j = 0;while (cin >> n){memset(A, 0, sizeof(A)), memset(C, 0, sizeof(C));sum = 0, tot = 0;Input();aver = tot/n;Creat_C();sort(C, C+n);Calculate();cout << sum << endl;}}后两个AC是测试A,C数组的。
1 0
- uva - 11300 - Spreading the Wealth(数学推导,中位数)
- UVA 11300 - Spreading the Wealth(数学推导+中位数)
- UVa 11300 Spreading the Wealth (数学推导-中位数)
- UVa Spreading the Wealth-11300(数学推导+中位数)
- uva 11300Spreading 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(中位数)
- 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(中位数)
- UVA 11300 Spreading the Wealth 中位数 *
- Uva 11300 - Spreading the Wealth(中位数距离)
- 钟摆效果动画效果
- 部署Office Web Apps Server并配置其与SharePoint 2013的集成
- Android设计:.9.png图片的制作方法
- android全局异常检测并处理
- Java多线程之线程间协作 notify与wait的使用
- uva - 11300 - Spreading the Wealth(数学推导,中位数)
- 3.5.8 理解GC日志
- 叉包舍-DIY之无线网络连接受限故障分析
- 一直没用过 new try 记录一下
- MVC+DAO设计模式下的设计流程
- svn merge和branch
- servlet以及jsp中编码设置
- Qt常用类
- ubuntu安装深度软件中心