uva11300 - Spreading the Wealth (中位数)
来源:互联网 发布:sql语句修改表结构 编辑:程序博客网 时间:2024/06/05 04:16
题意:
n个人(1,2,…n),围成圈站(即第一个人左边是最后一个人),每个人有一定金币数,可将金币转移左右相邻的人,合理分配使得每个人的金币数相同,求分配过程中需转移金币的最小值。
思路:
1 给 2 x2 个金币
2 给 3 x3 个金币
k 给 k+1 k+1个金币
n 给 1 x1 个金币
Aver = a1 - x2 + x1 -> x2 = aver -a1 + x1
Aver = a2 - x3 + x2 -> x3 = aver -a2 + x2
Aver = a3 - x4 + x3 -> x4 = aver -a3 + x3
Aver = a4 - x5 + x4 -> x5 = aver -a4 + x4
……
ci为∑aj- Aver
cnt = |x1|+|x2|+|x3|+…+|xn|=|x1 - 0| + |x1 - c1| + |x1 - c2|+...+|x1- cn-1|
则该问题即为 求x1使得 x1到这些点的距离之和最小 模型
-> 即求ci的中位数
代码:
#include <iostream>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;long long arr[1000005],temp[1000005];int main() {int n;while (scanf("%d", &n)==1) {int i;long long sum=0,t, cnt=0, aver;for (i = 1; i <= n; i++){scanf("%lld",&arr[i]);sum += arr[i];}aver = sum / n;temp[0] = 0;for (i = 1; i < n; i++){temp[i] = temp[i - 1] + aver - arr[i+1];}sort(temp,temp+n);t = temp[n/2];for (i = 0; i < n; i++){cnt += abs(t - temp[i]);}printf("%lld\n",cnt );}}
0 0
- 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
- 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 11300Spreading the Wealth(数学 中位数)
- UVA 11300-Spreading the Wealth(中位数)
- UVA - 11300 Spreading the Wealth 公式+中位数
- Codeforces Round #340 (Div. 2)——D
- C++ primer P81 练习3.5
- 2015年第六届蓝桥杯C/C++程序设计本科B组省赛 加法变乘法(结果填空)
- HDU 5612 Baby Ming and Matrix games(dfs搜索)
- Codeforces Round #340 (Div. 2)-B. Chocolate(组合数学的乘法计数)
- uva11300 - Spreading the Wealth (中位数)
- IOS绘制虚线
- 矩阵和散列表
- console.log
- Android四大组件
- Codeforces Round #340 (Div. 2)题解
- 结构化编译器前端 Clang 介绍
- [ssh新闻发布系统四]使用富文本编辑器发布新闻
- Java 高级类(上) —— 抽象类和接口