二分贪心练习题U21
来源:互联网 发布:java音乐播放器源代码 编辑:程序博客网 时间:2024/05/16 08:02
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
65 2 4 1 7 50Sample Output
Set #1The minimum number of moves is 5.
题意:
水题一道,就是算出将数列柱子变成一样高,要动多少次。
分析:
算出平均高度,在循环找出低于平均高度的列,累加高出的部分总和
#include<iostream>#include<vector>#include<string>#include<set>#include<map>#include<algorithm>#include<string.h>using namespace std;int main(){ int m,n,sum=0,ave=0,h=0,b=1; vector<int>a; while(1) { cin>>n; if(n==0){break;} for(int i=0;i<n;i++) { cin>>m; sum=sum+m; a.push_back(m); } ave=sum/n; for(int i=0;i<n;i++) { if(a[i]>ave) { h=h+(a[i]-ave); } } cout<<"Set #"<<b<<endl; cout<<"The minimum number of moves is "<<h<<"."<<endl<<endl; a.clear();b=b+1; sum=0;h=0;ave=0; }}
0 0
- 二分贪心练习题U21
- 二分贪心练习题-B2
- 二分贪心练习题-E5
- 二分贪心练习题-T20
- 二分贪心练习题-V22
- 二分贪心练习题-X24
- 二分贪心练习题-Y25
- 二分贪心练习题D-4
- 二分贪心练习题专题总结
- 贪心练习题
- 二分贪心练习题--D(四个数和为0的组合数)
- 二分+贪心
- 贪心 + 二分
- 贪心二分
- 贪心+二分
- 二分查找练习题
- 二分图匹配练习题
- 贪心(bnuoj49103+二分+贪心)
- 【Codeforces Round #411 (Div. 1)】Codeforces 804D
- 超酷的 Vim 搜索技巧
- Vuforia核心特征视频教程
- 《Effective java 第2版》读书笔记--泛型与枚举
- 抓取某网站数据小结
- 二分贪心练习题U21
- ICP算法理解
- javaScript & jquery完美判断图片是否加载完毕
- poj 2387 Til the Cows Come Home
- android中的finish()
- log(n!)与nlogn是等价无穷大
- 常用排序算法-快速排序
- 静态
- java.util.Date和java.sql.Date的区别和相互转化