http://acm.hdu.edu.cn/showproblem.php?pid=2088

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 consideration, 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 should not be processed.

 
Output
For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.
 
Sample Input
6
5 2 4 1 7 5
0
 
Sample Output
5
 
代码:

#include <bits/stdc++.h>
using namespace std; int a[111]; int main()
{
int n;
int num=0;
while(~scanf("%d",&n)) {
if(n==0) break;
num++;
if(num>1) printf("\n");
long long sum=0;
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
int ave=sum/n;
int cnt=0;
for(int i=1; i<=n; i++) {
if(a[i]>ave)
cnt+=a[i]-ave;
}
printf("%d\n",cnt);
}
return 0;
}

  

HDU 2088 Box of Bricks的更多相关文章

  1. HDU 2088 Box of Bricks(脑洞)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2088 Box of Bricks Time Limit: 1000/1000 MS (Java/Oth ...

  2. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

  3. HDOJ(HDU) 2088 Box of Bricks(平均值)

    Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...

  4. HDU 1326 Box of Bricks(思维)

    Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stac ...

  5. 杭电 2088 Box of Bricks

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2088 解题思路:一堆高度不同的砖块,需要把它们砌成一堵墙,即每一堆砖的高度相同(即砖的总数除以砖的堆数 ...

  6. HDU 1326 Box of Bricks(水~平均高度求最少移动砖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...

  7. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. Box of Bricks最小移动砖块数目

    Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and ...

  9. [POJ1477]Box of Bricks

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19503   Accepted: 7871 Description Litt ...

随机推荐

  1. PHP缓存锁原理及利用

    原文链接:https://blog.csdn.net/tim_phper/article/details/54949404 概述: 项目当中经常要考虑数据高并发的情况,为了避免并发导致出现一些资源重复 ...

  2. Vue入门2

    欢迎转载,转载请注明出处. 前言 学习本系列Vue知识,需要结合本系列的一些demo.你可以查看我的 Github 或者直接下载 ZIP包 . 建议学习本系列之前已经会一个其他的前端框架,了解计算属性 ...

  3. ethereumjs/ethereumjs-util

    ethereumjs/ethereumjs-util Most of the string manipulation methods are provided by ethjs-util 更多的字符串 ...

  4. Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs

    在部署工程时,没有加入javax.servlet-api.jar(3.0.1) 和 standard.jar(1.1.2)两个jar包导致此问题. 另外,如果原来容器中有javax.servlet-a ...

  5. web.xml配置遇到的问题

    web.xml<listener>            <listener-class>org.springframework.web.context.ContextLoad ...

  6. python:'ascii' codec can't encode character

    python默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错,python没办法处理非ascii编码的, 此时需要自己设置python的默认编码,一般设置为u ...

  7. 启动hbase shell报错:org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet

    查看日志发现:Waiting for dfs to exit safe mode 这说明HDFS目前处于安全模式,需要退出才行,于是进入Namdenode节点,执行命令: hdfs dfsadmin ...

  8. Hibernae

    开始尝试挺java ee的课程,马士兵老师的 1.ssh的整个框架体系 spring会贯穿在整个过程之中 2.Hibernate的整个框架体系 3. 4. 5. 6.

  9. 一、InnoDB引擎

    一.InnoDB的历史 MYSQL的5.1版本的时候还是使用旧的innoDB,当时orale公司推出的新的innoDB引擎, 但是需要以插件的形式编译,叫innoDB plugin : 知道MYSQL ...

  10. 奔跑吧Linux

    刚拿到MiZ702,就被他的"外貌"深深的吸引,核心板加底板的形式让她看上去,强大而神秘-- 华丽的外表之下是否有着与之相当的内含呢,我们拿Linux将其检验一番! 板载的TF卡里 ...