Box of Bricks最小移动砖块数目
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<stdio.h>
void sort(int *a,int n);
int a[];
void main()
{
int n;
while(scanf("%d",&n)== && n!=)
{
int i,j=,arev=;
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
arev+=a[i];
}
arev=arev/n;
sort(a,n);
while(a[n-]>arev)
{
if(a[n-]-arev>arev-a[])
{
j+=(arev-a[]);
a[n-]=a[n-]-(arev-a[]);
a[]=arev; }
else
if(a[n-]-arev==arev-a[])
{
j+=(arev-a[]);
a[n-]=arev;
a[]=arev; }
else
{
j+=(a[n-]-arev);
a[]=+a[]+a[n-]-arev;
a[n-]=arev; }
sort(a,n);
}
printf("%d\n\n",j); } }
void sort(int *a,int n)
{
int i,j;
for(i=;i<n-;i++)
for(j=i+;j<n;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
Box of Bricks最小移动砖块数目的更多相关文章
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- [POJ1477]Box of Bricks
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19503 Accepted: 7871 Description Litt ...
- HDOJ(HDU) 2088 Box of Bricks(平均值)
Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...
- HDOJ 1326 Box of Bricks(简单题)
Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...
- 591 - Box of Bricks
Box of Bricks Little Bob likes playing with his box of bricks. He puts the bricks one upon another ...
- Box of Bricks
Box of Bricks Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU 2088 Box of Bricks
http://acm.hdu.edu.cn/showproblem.php?pid=2088 Problem Description Little Bob likes playing with his ...
- HDU 2088 Box of Bricks(脑洞)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2088 Box of Bricks Time Limit: 1000/1000 MS (Java/Oth ...
随机推荐
- 刻意练习,逃离舒适区——怎么样成为一个高手[罗辑思维]No.183_知识笔记
2016/10/30 14:31:32 一.对事物的见解分为两类: 1.评论性的见解 说的内容都是对的,符合常理的,但是却是不解决问题的. ...
- mysql和oracle的mybatis操作
1.Oracle.MySQL插入时返回下一个主键的操作 Oracle:<insert id="insert" parameterClass="ROLE"& ...
- CODEVS1291 火车线路
http://codevs.cn/problem/1291/ 题目描述 Description 某列火车行使在C个城市之间(出发的城市编号为1,结束达到的城市的编号为C),假设该列火车有S个座位,现在 ...
- note name
谦谦君子:借用<周易·谦>:“初六,谦谦君子,用涉大川,吉.” 温润如玉:化用<诗经·卫风·淇奥>“有匪君子,如切如磋,如琢如磨”之义.
- tool debug Android phonegap app
phonegap debug 最近发现了一个可以调试phonegap的工具 在Google浏览器上调试Android真机的APP 这是好啊!!!跟Mac上的Safari 浏览器一样调试iOS 的A ...
- Ax 从一个form关闭另外一个form,AX全局变量
如果这个两个form存在调用关系,我们当然可以在调用的时候把对象传过来,然后再关闭之. 但是当2个form没有被调用的关系,我们可以利用infolog.globalCache()将FORM对象保存起来 ...
- 【IHttpHandler】HttpModule,HttpHandler,HttpHandlerFactory简单使用
这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后.应 ...
- 1.1Android系统架构
Android目前是一个非常优秀的嵌入式系统,具有非常完善的系统架构! Android系统架构分为4层:(从下至上) 第一层:Linux内核层,包括驱动程序以及管理内存.进程.电源等资源的程序 因为A ...
- DPDK内存管理-----(二)rte_mempool内存管理
DPDK以两种方式对外提供内存管理方法,一个是rte_mempool,主要用于网卡数据包的收发:一个是rte_malloc,主要为应用程序提供内存使用接口.本文讨论rte_mempool.rte_me ...
- c#自定义进度条
有些时候我们做的程序需要进度条,而vs提供的控件不是我们想要的.先看效果图: 进度条闪烁动画,当然背景可设为Transparent 之前想手绘进度条线条的,结果控件运行时会闪烁,所以直接用 ...