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. jmeter随笔(11)--上传文件接口出错

    点击标题下「飞测」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期 ...

  2. 【Unity Shaders】学习笔记——SurfaceShader(三)BasicDiffuse和HalfLambert

    [Unity Shaders]学习笔记——SurfaceShader(三)BasicDiffuse和HalfLambert 转载请注明出处:http://www.cnblogs.com/-867259 ...

  3. Facebook等使用苹果源生分享

         1.Facebook官方的SDK分享               2.ShareSDK,第三方集成的分享方式      3.网页分享方式分享      4.IOS6之后,苹果自己集成了对于F ...

  4. 跟我学 NHibernate (一)

    NHibernate 是一个强大的 ORM 框架,本博文主要就 NHibernate 的使用方法及语法做一些简单的介绍.  1.NHibernate 语法   新建一个类,命名为: QueryCrit ...

  5. 华为OJ平台——字符串通配符

    题目描述: 在计算机中,通配符一种特殊语法,广泛应用于文件搜索.数据库.正则表达式等领域.现要求各位实现字符串通配符的算法.要求:实现如下2个通配符: *:匹配0个或以上的字符(字符由英文字母和数字0 ...

  6. Lotus防病毒与数据备份案例

    Lotus防病毒与数据备份案例 上文(http://chenguang.blog.51cto.com/350944/1334595)中我们已安装好了Domino服务器,这节里我们需要考虑安全解决方案, ...

  7. kickstrat

    定制部分 硬盘分区 root密码 网络地址 公共部分 %pre% %post% tcp调优 集中配置程序(ansible) sudo配置

  8. Qt事件处理机制

    研一的时候开始使用Qt,感觉用Qt开发图形界面比MFC的一套框架来方便的多.后来由于项目的需要,也没有再接触Qt了.现在要重新拾起来,于是要从基础学起. Now,开始学习Qt事件处理机制. 先给出原文 ...

  9. Unieap3.5-前台js判断表单必录

    //用户信息字段检查 var custFrm=unieap.byId('custFrm'); var isValid=custFrm.validate(true); if(!isValid){ ret ...

  10. 在.NET2.0中解析Json和Xml

    在.NET解析json有很多方法,这里介绍最简单也用的最多的一种. 一.添加引用 解析Json,先下载开源控件 Newtonsoft.Json.dll 下载地址:http://files.cnblog ...