传送门:

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

Box of Bricks

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20219    Accepted Submission(s): 6473

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
 
Author
qianneng
 
Source
 
分析:
当时确实没有想到
:小于平均的栈其差多少的和就是需要移动的总数
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max_v 50
int a[max_v];
int main()
{
int n,k=;
while(~scanf("%d",&n))
{
if(n==)
break;
if(k)
printf("\n");//注意输出格式,最后一个测试不能有空行
int sum=;
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
sum/=n;//平均
int c=;
for(int i=; i<n; i++)
{
if(a[i]<sum)//小于平均的栈其差多少的和就是需要移动的总数
c+=(sum-a[i]);
}
printf("%d\n",c);
k++;
}
return ;
}
 

HDU 2088 Box of Bricks(脑洞)的更多相关文章

  1. HDU 2088 Box of Bricks

    http://acm.hdu.edu.cn/showproblem.php?pid=2088 Problem Description Little Bob likes playing with his ...

  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. python的爬虫

    requests库的安装 https://blog.csdn.net/xiaokuang5020/article/details/80580631 Response对象属性 属性 说明 r.statu ...

  2. winform多线程调用控件

    对多线程操作控件的理解: 控件不能被非创造他的线程修改.需调用控件.beginvoke,注入UI线程.控件.beginvoke会把操作加入UI线程,阻塞画面响应.不要把耗时的计算放在控件.beginv ...

  3. javascript判断一个元素是另一个元素的子元素

    function isParent (obj,parentObj){ while (obj != undefined && obj != null && obj.tag ...

  4. 表单校验常用原生js库

    1.字符串去除左右空格继承形式// 除去左右空格String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, & ...

  5. web前端优化之内容优化

    前端内容优化主要有以下几条: 1.尽量减少http请求 (1)合并文件,把多个css文件合并在一起: (2)css Sprites,把css相关的background元素进行背景图绝对定位: (3)图 ...

  6. Java Jsp使用

    1.Jsp基础 1)Jsp的执行过程 tomcat服务器完成:jsp文件->翻译成java文件->编译成class字节码文件-> 构造类对象-> 调用方法 tomcat的wor ...

  7. Apache服务器运维笔记(2)----使用apxs来进行编译安装 mod_txt 模块

    mod_txt是一个非常有趣的模块,它实现了文本的输出过滤器,它可以在指定类型的网页上,将你指定的txt文件显示在网页的头部和尾部. 在它的主页 http://apache.webthing.com/ ...

  8. webgis开发-开始向JS转向

    官方的一个blog Final Release and Support Plan for the ArcGIS APIs / Viewers for Flex and Silverlight 网址: ...

  9. 沙盒 sandbox 简记随笔

    沙盒又称沙箱(sandbox),是一种按照  安全策略  限制  程序行为  的  执行环境. “沙盒”技术的实践运用流程是: 1. 让疑似病毒文件的可疑行为在虚拟的“沙盒”里充分运行,“沙盒”会记下 ...

  10. 定制化移动办公APP:打造企业专属的“钉钉”“纷享销客”,实现企业办公管理一体化

    一.项目背景 随着信息化社会的高速发展,市场竞争日益激烈,传统的管理和办公系统多且复杂,用户需要使用多个系统才可完成一项工作,而且各个系统的界面和风格存在差异,造成了信息查找不便,大大降低了用户的工作 ...