Algorithm title: UVa 591 Box of Bricks (analog)

Source: Internet
Author: User

591-box of Bricks

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php? Option=com_onlinejudge&itemid=8&category=467&page=show_problem&problem=53 2

Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different. ' Look, I ' ve built a wall! ' and 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, she's right. So it sets out to rearrange of the bricks, one by one, such which all stacks are the same height afterwards. But since Bob is lazy him wants to does this with the minimum number of bricks. Can you help?

and.

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

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

Output

For each set, the "number of the" set, the as shown in the sample output. Then Print the "minimum number ofmoves is K.", where k is the minimum number of bricks so have to B E 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.

Water.

Complete code:

/*0.016s*/
  
#include <cstdio>
  
int a[50];
  
int main (void)
{
    int n, i, case = 1, sum, AVR, ans;
    while (scanf ("%d", &n), N)
    {
        sum = 0;
        Ans = 0;
        for (i = 0; i < n; i++)
        {
            scanf ("%d", &a[i]);
            Sum + + a[i];
        }
        AVR = sum/n;
        for (i = 0; i < n; i++)
            if (A[i] > AVR)
                ans + = (A[i]-AVR);
        printf ("Set #%d\nthe minimum number of moves is%d.\n\n", case++, ans);
    return 0;
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.