wenbao与中位数

 

分金币

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 }

 

 

 

 

 

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:54  wenbao  阅读(74)  评论(0编辑  收藏  举报