1poj1477(贪心)

来源:互联网 发布:win10关闭软件快捷键 编辑:程序博客网 时间:2024/05/17 17:56

http://poj.org/problem?id=1477

Box of Bricks
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 14471Accepted: 6079

Description

Little Bob likes playing withhis box of bricks. He puts the bricks one upon another and buildsstacks of different height. "Look, I've built a wall!", he tellshis older sister Alice. "Nah, you should make all stacks the sameheight. Then you would have a real wall.", she retorts. After alittle con- sideration, Bob sees that she is right. So he sets outto rearrange the bricks, one by one, such that all stacks are thesame height afterwards. But since Bob is lazy he wants to do thiswith the minimum number of bricks moved. Can you help?
1poj1477(贪心)


Input

The input consists of severaldata sets. Each set begins with a line containing the number n ofstacks Bob has built. The next line contains n numbers, the heightshi 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 ofstacks. Thus, it is always possible to rearrange the bricks suchthat all stacks have the same height.

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

Output

For each set, first print thenumber of the set, as shown in the sample output. Then print theline "The minimum number of moves is k.", where k is the minimumnumber of bricks that have to be moved in order to make all thestacks the same height.

Output a blank line after each set.

Sample Input

65 2 4 1 7 50

Sample Output

Set #1The minimum number of moves is 5.

Source

Southwestern European Regional Contest 1997
这题做过3遍了加上这一次。。
高度比平均值小的不动移动步数==sum(所有比平均高度高的墙-平均高度)(---->大水之移花接木)
#include<stdio.h>
int a[60];
int main()
{
    intn,flag=1,average,sum=0;
   while(scanf("%d",&n)!=EOF)
    {
       if(n==0)
           break;
       int count=0,i;
       sum=0;average=0;
       for(i=0;i<n;i++)
       {
           scanf("%d",&a[i]);
           sum+=a[i];
       }
       average=sum/n;
       for(i=0;i<n;i++)
       {
           if(a[i]>average)
           {
               count+=a[i]-average;
           }
       }
       printf("Set #%d\n",flag++);
       printf("The minimum number of moves is %d.\n\n",count);
    }
    return0;
}
原创粉丝点击