UVA 591 Box of Bricks

Source: Internet
Author: User

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, should make all stacks the same height. Then you would has a real wall. ", she retorts. After a little con-sideration, Bob sees that she was right. So he sets out to rearrange the bricks, and one by one, such and all stacks is the same height afterwards. But since Bob was the lazy he wants to does this with the minimum number of bricks moved. Can help?

Input
The input consists of several data sets. Each set begins with a line containing the number N of Stacks Bob have built. The next line contains n numbers, the Heights hi of the n stacks. Assume 1?≤N≤? - and 1≤? h i ?≤ - .

The total number of bricks is divisible by the number of stacks. Thus, it's always possible to rearrange the bricks such, and all stacks has the same height.

The input is terminated by a set starting with n = 0. This set should is 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 are K.", where k is the minimum number of bricks that has the to being 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 intmaxn= -;typedef Long LongLL;intA[MAXN];intMain () {#ifndef Online_judgeFreopen ("Text//in.txt","R", stdin);#endif //Online_judge     intNintcas=1; while(~scanf("%d", &n) &&n) {intsum=0, high=0; for(intI=0; i<n;i++) {scanf("%d", &a[i]);        Sum+=a[i]; }intavg=sum/n;intcn1=0; for(intI=0; i<n;i++) {if(A[I]&LT;AVG) ans+=avg-a[i]; }printf("Set #%d\nthe minimum number of moves is%d.\n\n", Cas++,ans); }return 0;}/*sample input212-3646397sample output72701*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 591 Box of Bricks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.