zoukankan      html  css  js  c++  java
  • F

    There arenbuckets on the ground, where thei-th bucket containsaistones. Each time one can performone of the following two operations:
     
        1.Remove a stone from one of the non-empty buckets.
        2.Move a stone from one of the buckets (must be non-empty) to any other bucket (can be empty).
     
    What’s the minimum number of times one needs to perform the operations to make all the buckets containthe same number of stones?
     
    Input
    There are multiple test cases. The first line of the input contains an integerT, indicating the number oftest cases. For each test case:
    The first line contains an integer n(1<=n<=1e5), indicating the number of buckets.
    The second line containsnintegersa1; a2;... ; an(0<=a<=1e9), indicating the number of stones in thebuckets.
    It’s guaranteed that the sum ofnof all test cases will not exceed106.
     
    Output
    For each test case output one line containing one integer, indicating the minimum number of times neededto make all the buckets contain the same number of stones.

    Sample Input

    4
    3
    1 1 0
    4
    2 2 2 2
    3
    0 1 4
    1
    1000000000

    Sample Output

    2
    0
    3
    0

    Hint

    For the first sample test case, one can remove all the stones in the first two buckets.

    For the second sample test case, as all the buckets have already contained the same number of stones, no operation is needed.

    For the third sample test case, one can move 1 stone from the 3rd bucket to the 1st bucket and then remove 2 stones from the 3rd bucket.

    代码
     
     1 #include<iostream>
     2 using namespace std;
     3 const int maxn=1e5+10;
     4 int n,a[maxn];
     5 int main(){
     6     int t;
     7     scanf("%d",&t);
     8     while(t--){
     9         long long count=0;
    10         long long sum=0;
    11         scanf("%d",&n);
    12         for(int i=0;i<n;i++){
    13             scanf("%d",&a[i]);
    14             sum+=a[i];
    15         }
    16         long long ave=sum/n;
    17         for(int i=0;i<n;i++){
    18             if(a[i]<ave) count+=(ave-a[i]);
    19         }
    20         count+=(sum-ave*n);
    21         printf("%lld
    ",count);    
    22     } 
    23 }
     
  • 相关阅读:
    GitHub 优秀的 Android 开源项目
    Android SDK代理服务器解决国内不能更新下载问题
    python拓展3 常用算法
    HTML+CSS实现页面
    数据库入门4 结构化查询语言SQL
    2016年蓝桥杯预选赛试题(水题)
    linux操作系统3 vi编辑器
    python应用之爬虫实战2 请求库与解析库
    python应用之爬虫实战1 爬虫基本原理
    SQL常用条件操作符
  • 原文地址:https://www.cnblogs.com/-happy-/p/12517986.html
Copyright © 2011-2022 走看看