zoukankan      html  css  js  c++  java
  • 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
    ", cot);
    27     }
    28     return 0;
    29 }

    只有不断学习才能进步!

  • 相关阅读:
    CControlLayer
    CBiontCache
    CHero
    CWidgetMgr---cpp
    CWidgetMgr---H
    CXAnimation类
    CXAnimation.h动画类
    CXCommon.h工具类
    【leetcode】441. Arranging Coins
    【linux基础】关于ARM板子使用O3编译选项优化
  • 原文地址:https://www.cnblogs.com/wenbao/p/6395194.html
Copyright © 2011-2022 走看看