zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 98 (Rated for Div. 2)B-Toy Blocks

    									B. Toy Blocks
    									time limit per test2 seconds
    									memory limit per test256 megabytes
    									inputstandard input
    									outputstandard output
    

    You are asked to watch your nephew who likes to play with toy blocks in a strange way.

    He has n boxes and the i-th box has ai blocks. His game consists of two steps:

    he chooses an arbitrary box i;
    he tries to move all blocks from the i-th box to other boxes.
    If he can make the same number of blocks in each of n−1 other boxes then he will be happy, otherwise, will be sad. Note that your nephew can only move the blocks from the chosen box to the other boxes; he cannot move blocks from the other boxes.
    You don’t want to make your nephew sad, so you decided to put several extra blocks into some boxes in such a way that no matter which box i he chooses he won’t be sad. What is the minimum number of extra blocks you need to put?

    Input
    The first line contains a single integer t (1≤t≤1000) — the number of test cases.

    The first line of each test case contains the integer n (2≤n≤105) — the number of boxes.

    The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109) — the number of blocks in each box.

    It’s guaranteed that the sum of n over test cases doesn’t exceed 105.

    Output
    For each test case, print a single integer — the minimum number of blocks you need to put. It can be proved that the answer always exists, i. e. the number of blocks is finite.

    Example
    inputCopy
    3
    3
    3 2 2
    4
    2 2 3 2
    3
    0 3 0
    outputCopy
    1
    0
    3
    Note
    In the first test case, you can, for example, put one extra block into the first box and make a=[4,2,2]. If your nephew chooses the box with 4 blocks, then we will move two blocks to the second box and two blocks to the third box. If he chooses the box with 2 blocks then he will move these two blocks to the other box with 2 blocks.

    In the second test case, you don’t need to put any extra blocks, since no matter which box your nephew chooses, he can always make other boxes equal.

    In the third test case, you should put 3 extra blocks. For example, you can put 2 blocks in the first box and 1 block in the third box. You’ll get array a=[2,3,1].

    题意:
    有 n 个盒子,每个盒子里面有 n 个物品,如果说可以使得任选一个盒子将这个盒子里面的所有球分到其他盒子里面,使得其他每个盒子里面的物品数量相等,这个时候可能不满足要求。需要加上一些物品,问最少加多少个物品使得这个操作成立,每次加的时候,加物品的时候可以加到不同的盒子中
    比如说,有三个盒子,分别有2 2 3个球
    这时候可以选择 加一个球到 3 个物品的盒子中,这时候
    无论选 2 还是4都可以使得,其他盒子中物品的数量相等

    主要的思想就是对(n - 1)中的每一个都要使得他们相等,这就要想对剩下的每一堆取一个平均值,但是平均值来讲可能并不满足是个整数,这就要对其进行上取整,不足的通过添加来进行弥补,最终求得的需要的减去总和就是答案
    Code:
    第一种写法

        int T = read;
        while(T --){
            int n = read;
            ll sum = 0,maxx = 0;
            for(int i=1;i<=n;i++){
                a[i]=read;
                sum += a[i];
                maxx = max(maxx,a[i]);
            }
            ll t1 = sum + (n-1) - 1;
            t1 /= (n-1);
            t1 *= (n - 1);///上取整
            ll t2 = maxx * (n - 1);///最坏的方法
            cout << max(t1,t2) - sum <<endl;
        }
    

    换一个姿势写一波:

    int n = read;
    long long sum = 0;
    ll maxx = -1;
    
    for(int i = 1;i <= n; i ++){
        a[i] = read;
        sum += a[i];
        maxx = max(maxx,a[i]);
    }
    ll cnt = 0;
    cnt = sum / (n - 1);
    if(sum % (n - 1)) cnt ++;///上取整
    cout<<max(maxx * (n - 1) , cnt * (n - 1)) - sum<<endl;
    
  • 相关阅读:
    2、ansilbe常用模块详解及inventory介绍
    1、Ansible简介及简单安装、使用
    dhcp、tftp及pxe简介
    php-fpm常见错误
    php之编译安装
    nginx之常见错误
    ssh之秘钥登陆
    MySQL之主从复制
    Python之虚拟环境
    php调试工具之firePHP
  • 原文地址:https://www.cnblogs.com/PushyTao/p/14507421.html
Copyright © 2011-2022 走看看