HDOJ(HDU) 2088 Box of Bricks(平均值)
Problem 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
就是输入一组数字(代表砖),让它全部只能有平均值那么高,问移动的最少次数。(保证平均数是整数)
一次只能移动一个。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=0;
while(sc.hasNext()){
int n =sc.nextInt();
if(n==0){
break;
}
if(t==0){
t=1;
}else{
System.out.println();
}
int a[] = new int[n];
long sum=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
sum+=a[i];
}
int avg = (int)sum/n;
int num=0;
for(int i=0;i<n;i++){
if(a[i]>avg){
num+=a[i]-avg;
}
}
System.out.println(num);
}
}
}
HDOJ(HDU) 2088 Box of Bricks(平均值)的更多相关文章
- HDU 2088 Box of Bricks(脑洞)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2088 Box of Bricks Time Limit: 1000/1000 MS (Java/Oth ...
- 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)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- HDU 1326 Box of Bricks(思维)
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stac ...
- 杭电 2088 Box of Bricks
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2088 解题思路:一堆高度不同的砖块,需要把它们砌成一堵墙,即每一堆砖的高度相同(即砖的总数除以砖的堆数 ...
- HDU 1326 Box of Bricks(水~平均高度求最少移动砖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...
- HDOJ(HDU) 2143 box(简单的多次判断-用的卫条件)
Problem Description One day, winnie received a box and a letter. In the letter, there are three inte ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1326 Box of Bricks(简单题)
Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...
随机推荐
- Android(java)学习笔记231:服务(service)之混合方式开启服务
1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService: oncreate() ---> onstartCommand() ---& ...
- Codeforces 526D - Om Nom and Necklace 【KMP】
ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...
- python实现登录函数,比较简单
一个简单的python实现登录以及修改密码的函数 #密码错误3次,锁定登录: password_list = ['] def account_login(): Tries = 3 while Trie ...
- 【转】iOS中流(Stream)的使用
转自:http://southpeak.github.io/blog/2014/07/17/ioszhong-liu-stream-de-shi-yong/流提供了一种简单的方式在不同和介质中交换数据 ...
- Android签名详解(debug和release)
Android签名详解(debug和release) 1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- HTML基础语句
一,网页基础结构: 1 <html> 2 <head> 3 <title>我的第一个网页</title> 4 </head> 5 <b ...
- 关于asp.net程序连接不了ORACLE数据库而PL/SQL可以连接的问题
今天在发布ASP.NET WEB网站时发现程序连接不了数据 报“ORA-12154: TNS: 无法解析指定的连接标识符”的错误,但PL/SQL连接又没有问题.真莫名其秒.在百度找了好多相关的问题.都 ...
- 系统spt_values表--生成时间方便left join
时间处理我给你提供一个思路 系统有个spt_values表,可以构造一整个月的日期,然后左连接你统计好的数据,用CTE表构造多次查询 spt_values的超级经典的应用 http://www. ...
- NHibernate执行原生带sum的sql
可以自行查出想要的字段,并且返回的是model类型,可以点出所有的字段. 但是select 后面的字段名 必须有xml的字段名一模一样. ISession iSession = null; strin ...