HDOJ 2088

2024-03-21 15:20
文章标签 hdoj 2088

本文主要是介绍HDOJ 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

Author
qianneng

Source
冬练三九之二

解题思路
大致的可以想象成,将高过平均值的砖块放置到低于平均值的那条上。根据题意,这些砖块总是会有一个等于整数的均值,这样就方便多了,只需要计算有哪些砖块高于均值即可。

AC

#include<stdio.h>
using namespace std;
int main() {int hi[55],n;while (scanf("%d", &n) != EOF && n != 0) {int cnt = 0,avg;for (int i = 0; i < n; i++) {scanf("%d", &hi[i]);cnt += hi[i];}avg = cnt / n;cnt = 0;for (int i = 0; i < n; i++) {if (hi[i] > avg)cnt += hi[i] - avg;}printf("%d\n", cnt);}return 0;
}

这篇关于HDOJ 2088的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/833025

相关文章

hdoj 2371 decoded string. Decode the Strings

http://acm.hdu.edu.cn/showproblem.php?pid=2371 题意:给出编码的原则,给一个字符串,输出该字符串经过m次解码后的字符串。 啊啊啊啊。。。。无耻的看错题意了,以为给出字符串输出经过m次解码的后的字符串,样例死活过不了,赛后才发现问的是“decoded string”. 即解码后的字符串。。 思路:对于 5 3 2 3 1 5 4 helol

HDOJ 1874 畅通工程续——结构体模拟邻接链表的SPFA算法

Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。   Input 本题目包含多组数据,请处理到文

九度OJ-1435-迷瘴(HDOJ-2570)

题目地址:点击打开链接 题目描述: 通过悬崖的yifenfei,又面临着幽谷的考验—— 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死。 幸好yifenfei早有防备,提前备好了解药材料(各种浓度的万能药水)。现在只需按照配置成不同比例的浓度。 现已知yifenfei随身携带有n种浓度的万能药水,体积V都相

HDoj Integer Inquiry(大数)

真心要哭了。。这几天在搞大数  高精度计算  昨晚在机房敲 很快敲完了  就是过不了啊过不了  劳资都想骂脏话啊   NMB  一开始不输出前面的0啊 过不了  看discuss 百度 找了个AC的代码 找了几组测试数据  那个代码输出前面的0啊  我的妈  今天有找了个代码 不输出0啊 我的天。。。真心要被逼疯了  幸好还是AC了。。。算是有进步吧  之前的心态肯定坚持不下来啊

hdoj Least Common Multiple--最大公约数和最小公倍数

解题思路:求两个数的最小公倍数=两个数相乘,再处理最大公约数。最大公约数用辗转相除术。 最大公约数和最小公倍数说明见下面连接: https://jingyan.baidu.com/article/0964eca21e03ac8285f53602.html http://blog.csdn.net/qq_31828515/article/details/51812154 #include <

hdoj 1003 Max Sum---动态规划,最大子序列求和

初来乍到,动态规划不会呀,刚开始用暴力法,超时了!超时代码如下: 思路:大致是选中一个数当做结尾,然后加和。例如在6 -1 5 8 -7选中5作为结尾大致有如下序列 6  -1   5     -1   5           5 就这样遍历所以的结尾,结果超时。代码如下: #include<iostream>#include<algorithm>using namespace s

HDOJ 1078

标准的DAG上的DP,其实之前一直不大想得明白为什么dp[i][j]为什么一定是在(i,j)状态上的局部最优解了呢? 其实仔细想想和我一般所做的DP是一个道理,因为运用dfs的方法,因此可以确定的是,得到了dp[i][j]的值并且已经退出了(i,j)这个状态,就可以认为已经将(i,j)所有的后继的状态的最优解已经计算出了。而记忆化搜索就是可以看作剪枝的手段。其实这么一想貌似还没什么问题了。 个

HDOJ 2955

这道背包题和我们常见的背包题有所不同。如果根据以前做背包的惯性思维和题中数据的迷惑,会把概率乘以100来当作容量。但是经测试是不行的。 我们不妨换种思路,看做DAG上的DP思想。将所有有可能达到的钱的最大“逃跑”概率算出来,最后再将能够达到的最大的钱输出。而能不能够达到这个可以将所有除0以外的值初始化为0.意为逃跑的概率为0。 #include<cstdio>#include<cstring>

HDOJ 3398

这个题坑了太久太久啊!!!!!贡献了得有30+WA才发现 原来是因为在乘法中有溢出导致一直TLE啊。。。。 但是到最后也不知道有个问题怎么解决的。 就是在getp()中的num值的诡异的改变!   #include<stdio.h>#include<string.h>#include<math.h>typedef long long LL;const int TON=20100501;c

HDOJ 1348 基本二维凸包问题

这次写的凸包用的是Graham scan算法 就数据结构上只是简单地运用了一个栈 #include<stdio.h>#include<cmath>#include<algorithm>//#define LOCALusing namespace std;const int max1=1000;typedef struct point{int x;int y;}point;