• Box of Bricks


    Box of Bricks

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 63   Accepted Submission(s) : 20
    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. ``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? [center][img]../../data/images/1326-1.gif[/img][/center]
     
    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.
     
    Source
    Southwestern Europe 1997
     
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int main()
     5 {
     6     int n,a[100],i,sum,sign,j=1;
     7     scanf("%d",&n);
     8     while(1)
     9     {
    10         for(i=0,sum=0;i<n;i++)
    11         {
    12             scanf("%d",&a[i]);
    13             sum+=a[i];
    14         }
    15         sum/=n;
    16         for(i=0,sign=0;i<n;i++)
    17         {
    18             if(a[i]>sum)
    19             {
    20                 sign+=a[i]-sum;
    21             }
    22         }
    23         printf("Set #%d
    The minimum number of moves is %d.
    
    ",j,sign);
    24         j++;
    25         scanf("%d",&n);
    26         if(n==0)
    27             break;
    28     }
    29     return;
    30 }
    View Code
    转载请备注:
    **************************************
    * 作者: Wurq
    * 博客: https://www.cnblogs.com/Wurq/
    * Gitee: https://gitee.com/wurq
    **************************************
  • 相关阅读:
    [APIO2014]序列分割
    [HNOI2014]世界树
    [THUWC2017]随机二分图
    快乐游戏鸡
    [SHOI2014]三叉神经树
    带花树学习笔记
    最小树形图——朱刘算法学习笔记
    【WC2018】即时战略
    [HNOI2015]接水果
    [HAOI2018]染色
  • 原文地址:https://www.cnblogs.com/Wurq/p/3750244.html
Copyright © 2020-2023  润新知