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 }
     
  • 相关阅读:
    C#:反射
    静态和非静态类
    数据的存入取出(注册机方式)
    退出unity运行
    网络流基础
    欧拉回路
    博弈论问题
    洛谷P5304 [GXOI/GZOI2019] 旅行者
    [ZJOI2006]物流运输
    POJ3278 Catch that cow
  • 原文地址:https://www.cnblogs.com/-happy-/p/12517986.html
Copyright © 2011-2022 走看看