uva 591 Box of Bricks

Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. Look, I‘ve built a wall!‘‘, he tells his older sister Alice.Nah, you should make all stacks the same height. Then you would have a real wall.”, she retorts. After a little con- sideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?

Input

The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1?≤n≤?50 and 1≤?hi?≤100.

The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.

The input is terminated by a set starting with n = 0. This set should not be processed.

Output

For each set, first print the number of the set, as shown in the sample output. Then print the line “ The minimum number of moves is k.”, where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.

Output a blank line after each set.

Sample Input

6

5 2 4 1 7 5

0

Sample Output

Set #1

The minimum number of moves is 5.

Miguel A. Revilla

1998-03-10

#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int maxn=55;
typedef long long LL;
int a[maxn];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Text//in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n;
    int cas=1;
    while(~scanf("%d",&n)&&n){
        int sum=0,high=0;
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        int avg=sum/n;
        int ans=0;
        for(int i=0;i<n;i++){
            if(a[i]<avg)ans+=avg-a[i];
        }
        printf("Set #%d\nThe minimum number of moves is %d.\n\n",cas++,ans);
    }
    return 0;
}

/*
Sample Input
2
12
-3646397
Sample Output
7
2701
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 10:35:31

uva 591 Box of Bricks的相关文章

HDOJ 1326. Box of Bricks 纯水题

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5994    Accepted Submission(s): 2599 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up

POJ 1477. Box of Bricks 纯水题

Box of Bricks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19552   Accepted: 7889 Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. "Look, I've buil

Box of Bricks(南阳oj)

Box of Bricks 时间限制:1000 ms  |  内存限制:65535 KB 描述 Bob在垒砖块,但是他垒的有些乱,但是他很懒,所以想花费最少的体力来让他垒好的砖块一样高,所以你需要做的就是算出最少需要移动的砖块 输入 多组测试数据,以EOF结束,第一行是一个n(1<=n<=50)表示有n摞砖 第二行是n个正整数k(1<=k<=100)表示每摞砖的砖块数量 输出 对于每组输入数据有两行输出,第一行是一个Case #x,表示x组测试数据,第二行是一句话The minim

zoj 1251 Box of Bricks

Box of Bricks Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice.

Box of Bricks最小移动砖块数目

Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I've built a wall!”, he tells his older sister Alice. “Nah, you should make all stacks the same height. The

uva 12293 - Box Game(组合游戏)

题目链接:uva 12293 - Box Game 题目大意:有两个盒子,第一个盒子装有n个球,第二个盒子装又1个球,每次操作将少的盒子中的球全部拿掉,并从另一个盒子中取一些球放入该盒子,不能使另一个盒子中球的个数为0.两人轮流操作,问说最后谁胜. 解题思路:n如果为2i?1那么先手必败. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; bool judge (

杭电 HDU ACM 1326 Box of Bricks

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5457    Accepted Submission(s): 2358 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up

HDU 1326 Box of Bricks(简单数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older siste

UVA 12293 - Box Game(博弈)

UVA 12293 - Box Game 题目链接 题意:两个盒子,一开始一个盒子有n个球,一个只有1个球,每次把球少的盒子中球消掉,把多的拿一些球给这个盒子,最后不能操作的输(球不能少于1个),Alice先手,问谁赢 思路:博弈,题目其实可以转化为,给定一个n,每次把减少1到n/2的数字,最后谁是1谁就输了,那么可以去递推前几项找个规律,或者推理,都可以发现只要是2^i - 1的数字Bob就赢,否则Alice赢 代码: #include <stdio.h> #include <stri