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

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 con- sideration, 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, first print the number of the set, as shown in the sample output. Then print the line "The minimum number of moves is k.", where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.

Output a blank line after each set.

Sample Input

  1. 6
  2. 5 2 4 1 7 5
  3. 0

Sample Output

  1. Set #1
  2. The minimum number of moves is 5.

Source

THINKING

  贪心,先计算平均值,再扫描数组求出差值,注意句末的句号和组数之间的空行!

  1. var a:array[..] of longint;
  2. n,i,sum,summ,t:longint;
  3. begin
  4. summ:=;
  5. while true do
  6. begin
  7. fillchar(a,sizeof(a),);
  8. sum:=;
  9. t:=;
  10. readln(n);
  11. if n= then halt;
  12. for i:= to n do
  13. begin
  14. read(a[i]);
  15. inc(sum,a[i]);
  16. end;
  17. sum:=sum div n;
  18. for i:= to n do
  19. t:=abs(sum-a[i])+t;
  20. writeln('Set #',summ);
  21. writeln('The minimum number of moves is ',t div ,'.');
  22. writeln;
  23. inc(summ);
  24. end;
  25. end.

[POJ1477]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. Box of Bricks最小移动砖块数目

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

  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. HDOJ 1326 Box of Bricks(简单题)

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

  5. 591 - Box of Bricks

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

  6. Box of Bricks

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

  7. HDU 2088 Box of Bricks

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

  8. HDU 2088 Box of Bricks(脑洞)

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

  9. zoj 1251 Box of Bricks

    Box of Bricks Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Bob likes playing with his bo ...

随机推荐

  1. html+ashx 缓存问题

    最近采用html+ashx的方式做了一个项目的几个配置页面的功能,由于浏览器的缓存问题,每次更新数据提交后,页面总是不会刷新,也就是说除了第一次加载页面会向一般处理(ashx)拿数据外,其他情况都是优 ...

  2. 使用PHP导出Word文档的原理和实例

    PHP操作Word文档的方法有很多,这里再为大家提供一种方法. 原理   一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方 ...

  3. SQL学习_查询重复数据和连接多个表数据的方法

    进行数据库测试时需要根据不同场景查询数据,以便验证发现的问题是否为脏数据引起的.记录一下最近常用的查询方法: 1. 查询表中重复数据(id不同,多个字段值相同) select P1.* from pr ...

  4. OSG-3.4.0 简要说明(Readme)

    欢迎来到OpenSceneGraph(OSG)世界. Welcome to the OpenSceneGraph (OSG). 对于项目最新信息, 以及如何编译和运行库和示例的更多细节, 可以查看OS ...

  5. vi/vim正则表达式

    http://www.cnblogs.com/penseur/archive/2011/02/25/1964522.html 毋庸多言,在vim中正则表达式得到了十分广泛的应用. 最常用的 / 和 : ...

  6. Java应用程序可执行jar文件与服务器交互中文乱码

    生成可执行jar文件后,直接双击打开应用,发送Http请求带有中文时,服务器接收到的中文乱码! 解决方式: 1.在cmd命令中执行javaw命令打开jar可执行应用: 打开cmd命令框,输入: jav ...

  7. java实现的JDBCTemplate工具

    1.DButil import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impor ...

  8. 用android-x86模拟器不能运行程序错误Tag:libc的问题

    如果用的是x86的android模拟器,运行软件时一闪就关闭logcat中:类似:Fatal signal 11 (SIGSEGV) at 0x00000078 (code=1), thread 16 ...

  9. HDU 1160 FatMouse's Speed(DP)

    点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...

  10. POJ 3007 Organize Your Train part II(哈希链地址法)

    http://poj.org/problem?id=3007 题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的 ...