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?


InputThe 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. 
OutputFor 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

6
5 2 4 1 7 5
0

Sample Output

Set #1
The minimum number of moves is 5. 题意:使高度相同的最少移动次数
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int k=1;
while(scan.hasNext()){
int n=scan.nextInt();
if(n==0) break;
int a[]=new int[n];
int sum=0;
for(int i=0;i<n;i++) {
a[i]=scan.nextInt();
sum+=a[i];
}
int avg=sum/n,min=0;
for(int i=0;i<n;i++){
if(a[i]<avg) min+=(avg-a[i]);
}
System.out.println("Set #"+(k++));
System.out.println("The minimum number of moves is "+min+".");
System.out.println();
}
}
}

HDU 1326 Box of Bricks(思维)的更多相关文章

  1. HDU 1326 Box of Bricks(水~平均高度求最少移动砖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...

  2. HDOJ 1326. Box of Bricks 纯水题

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

  3. HDU 2088 Box of Bricks

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

  4. HDU 2088 Box of Bricks(脑洞)

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

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

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

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

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

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

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

  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. Git操作时遇到的一些问题和相应的处理方式

    Q1:如何解决冲突/避免冲突 A1:执行git fetch之后,本地可能会存在冲突. 如果希望合并本地修改内容,需要执行git merge.不过当有修改内容未提交时,不能merge,要么把修改内容提交 ...

  2. ALSA lib-ext plugin

    参考pcm_speex.c #include <stdio.h> #include <string.h> #include <unistd.h> #include ...

  3. 对Ajax返回的json数据做处理报错

    这个错误出现的原因是我再返回数据为json时,我页面的Ajax没有指定dataType: 'json'

  4. 55.ORM外键:引用同app下的不同模型,引用不同app下的模型,引用模型自身使用详解

    外键和表关系 外键是属于数据库级别的,在MySQL中,表有两种引擎,一种是InnoDB,另外一种是myisam.如果使用的是InnoDB引擎,是支持外键约束的.外键的存在使得ORM框架在处理表关系的时 ...

  5. 四、CentOS 7安装Oracle JDK

    CentOS 7安装Oracle JDK,查看Linux是否自带的JDK,如有openJDK,则卸载  CentOS7.1 JDK安装 1.卸载自带OPENJDK    用 java -version ...

  6. PHP csv文件30w+数据导入mysql数据库

    <?php class Add { public function data() { ini_set('memory_limit', '-1'); //PHP内存设置 $handle=fopen ...

  7. day02_1spring3

    面向切面编程.AOP手动代理和spring编写代理 一.什么是AOP 1.AOP简介: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 ...

  8. [NOI2015] 品酒大会 - 后缀数组,并查集,STL,启发式合并

    [NOI2015] 品酒大会 Description 对于每一个 \(i \in [0,n)\) 求有多少对后缀满足 LCP 长度 \(\le i\) ,并求满足条件的两个后缀权值乘积的最大值. So ...

  9. android toolbar 显示返回按钮并改变按钮颜色

    <android.support.design.widget.AppBarLayout android:id="@+id/about_appbar" android:layo ...

  10. c++指针实例

    #include <iostream> using namespace std; int main () { ; // 实际变量的声明 int* ip; // 指针变量的声明 ip = & ...