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 }
     
  • 相关阅读:
    @ControllerAdvice + @ExceptionHandler 使用
    将博客搬至CSDN
    Docker pull网络错误
    Centos7.5安装Docker
    Oracle18c创建不带C##的用户
    Centos7.5静默安装Oracle18c
    nodeJs和JavaScript的异同
    maven项目引入本地包,不使用中央仓库
    java中把指数形式的数字转为正常形式显示
    validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
  • 原文地址:https://www.cnblogs.com/-happy-/p/12517986.html
Copyright © 2011-2022 走看看