HDU1326: Box of Bricks

Source: Internet
Author: User

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 sister Alice. ''hannah, you shoshould make all stacks the same height. then you wowould 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 shoshould 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.

 

My idea is very simple. First, sort and then process it as follows:

 

[Cpp]
# Include <stdio. h>
# Include <algorithm>
Using namespace std;
 
Int main ()
{
Int n, cas = 1;
While (~ Scanf ("% d", & n), n)
{
Int a [100], I, sub = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
Sub + = a [I];
}
Sub = sub/n;
Sort (a, a + n );
Int sum = 0;
Printf ("Set # % d \ nThe minimum number of moves is", cas ++ );
While (a [n-1]> sub)
{
If (a [n-1]-sub> sub-a [0]) // when the maximum value reaches the average value, there are more than the smallest ones. Fill in the small ones, large statistics of the remaining
{
Sum + = sub-a [0];
A [n-1] = a [n-1]-(sub-a [0]);
}
Else if (a [n-1]-sub = sub-a [0]) // equals, the population is full.
{
Sum + = a [n-1]-sub;
A [n-1] = sub;
A [0] = sub;
}
Else // if the value is smaller than, the maximum value is first changed to the average value, and the excess value is moved to the minimum value.
{
Sum + = a [n-1]-sub;
A [n-1] = sub;
A [0] = a [0] + a [n-1]-sub;
}
Sort (a, a + n); // sort each time
}
Printf ("% d. \ n", sum );
}
 
Return 0;
}

# Include <stdio. h>
# Include <algorithm>
Using namespace std;

Int main ()
{
Int n, cas = 1;
While (~ Scanf ("% d", & n), n)
{
Int a [100], I, sub = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
Sub + = a [I];
}
Sub = sub/n;
Sort (a, a + n );
Int sum = 0;
Printf ("Set # % d \ nThe minimum number of moves is", cas ++ );
While (a [n-1]> sub)
{
If (a [n-1]-sub> sub-a [0]) // when the maximum value reaches the average value, there are more than the smallest ones. Fill in the small ones, large statistics of the remaining
{
Sum + = sub-a [0];
A [n-1] = a [n-1]-(sub-a [0]);
}
Else if (a [n-1]-sub = sub-a [0]) // equals, the population is full.
{
Sum + = a [n-1]-sub;
A [n-1] = sub;
A [0] = sub;
}
Else // if the value is smaller than, the maximum value is first changed to the average value, and the excess value is moved to the minimum value.
{
Sum + = a [n-1]-sub;
A [n-1] = sub;
A [0] = a [0] + a [n-1]-sub;
}
Sort (a, a + n); // sort each time
}
Printf ("% d. \ n", sum );
}

Return 0;
}

 

 

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.