码迷,mamicode.com
首页 > 其他好文 > 详细

wenbao与中位数

时间:2018-04-14 15:24:02      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:can   efi   i++   control   sort   strong   org   nba   printf   

 

分金币

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2275

 

将金币分给n个人,要求每个人的金币数相同且转移的最少

 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 using namespace std;
 5 const int maxn = 1e6+10;
 6 #define ll long long
 7 ll a[maxn], b[maxn], sum;
 8 int main(){
 9     int n;
10     while(scanf("%d", &n) == 1){
11         sum = 0;
12         for(int i = 1; i <= n; i++){
13             scanf("%lld", &a[i]);
14             sum += a[i];
15         }
16         sum /= n;
17         b[0] = 0;
18         for(int i = 1; i < n; i++){
19             b[i] = b[i-1] + a[i] - sum;
20         }
21         sort(b, b+n);
22         ll x = b[n/2], cot = 0;
23         for(int i = 0; i < n; i++){
24             cot += abs(x - b[i]);
25         }
26         printf("%lld\n", cot);
27     }
28     return 0;
29 }

 

 

 

 

 

 

只有不断学习才能进步!

 

wenbao与中位数

标签:can   efi   i++   control   sort   strong   org   nba   printf   

原文地址:https://www.cnblogs.com/wenbao/p/6395194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!