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最小移动砖块数目的更多相关文章

  1. HDOJ 1326. Box of Bricks 纯水题

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

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

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

  3. [POJ1477]Box of Bricks

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

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

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

  5. HDOJ 1326 Box of Bricks(简单题)

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

  6. 591 - Box of Bricks

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

  7. Box of Bricks

    Box of Bricks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  8. HDU 2088 Box of Bricks

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

  9. HDU 2088 Box of Bricks(脑洞)

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

随机推荐

  1. (续篇3):飞测独家のJmeter秘籍,限量发放

    好东西,分享大家,自上次分享出来fiddler导出jmx格式V4.0版本对外公开后,收到一些反馈,我们利用工作之余时间继续优化,现在一个比较稳定的版本出炉,分享给大伙,我们一起来看看. 特性说明: 版 ...

  2. 搭建Artifactory集群

    搭建Artifactory集群 制品仓库系统有很多,例如Artifactory.Archiva.Sonatype Nexus.Eclipse Package Drone,其中Artifactory拥有 ...

  3. [ Office 365 开发系列 ] 开发模式分析

    前言 本文完全原创,转载请说明出处,希望对大家有用. 在正式开发Office 365应用前,我们先了解一下Office 365的开发模式,根据不同的应用场景,我们选择最适合的开发模式. 阅读目录 Of ...

  4. 出现win8.1蓝屏重启故障该如何解决?

    出现win8.1蓝屏重启故障该如何解决?电脑出现win8.1蓝屏重启故障,系统提示“你的电脑遇到问题,需要重新启动,我们只收集某些错误信息,然后为你重新启动.”怎么解决?本教程为你提供了win8.1蓝 ...

  5. MSP430F149学习之路——AD

    代码一:Timer_A触发转换 #include <msp430x14x.h> void main() { WDTCTL = WDTPW + WDTHOLD; P6SEL |= BIT0; ...

  6. WWF3XOML方式创建和启动工作流 <第十篇>

    一.XOML使用工作流的好处 通过Xoml方式使用工作流的好处在于,它能够不重新启动程序的情况下,仅仅通过配置xoml就能够实现改变工作流,非常灵活. 创建一个WinForm程序如下: 代码如下: n ...

  7. C#使用Cookie方法

    //写入 protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie=new HttpCookie(&qu ...

  8. 查找Safari相关迹证

    日前有取证的同好提及Safari,想了解详细步骤,因而在此再补充说明相关. 除了Winodws外,Mac OS X也有为数不少的使用者,以下便以OS X自带的Safari浏览器为例,来查看有哪些重要迹 ...

  9. 操作系统模仿CMD

    实验一.命令解释程序的编写 专业:商软(2)班   姓名:列志华  学号:201406114254 一.        实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3 ...

  10. Bootstrap CSS 描述

    <!DOCTYPE html><html lang="zh-CN"><head> <!--定于内容,和内容的编码格式--> < ...