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 }
     
  • 相关阅读:
    zookeeper.Net
    转码和解码
    Node.js开发Web后台服务
    SQL Server 2012 读写分离设置
    使用C# 和Consul进行分布式系统协调
    http2
    .net core 2.1.3可能引发Could not load file or assembly XXXXX的错误
    jquery on函数和prop与attr区别
    html页面中完成查找功能
    C#匿名参数(转载too)
  • 原文地址:https://www.cnblogs.com/-happy-/p/12517986.html
Copyright © 2011-2022 走看看