HDU 1326

题意:给出一堆高度不一的砖头,现在使他们高度一样,问最少的移动次数,(每减少1就是移动一次)

思路:求出平均高度,然后模拟最后平均高度的数组,也就是说,每个数组对应每一个平均高度,也就是说比平均高度大的那些数据,就移动了,就这样直接求和

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int num[150];
 5 int main()
 6 {
 7     int n,t=1;
 8     while(cin >> n &&n)
 9     {
10         int tot=0;
11         for(int i=0;i<n;++i){
12             cin >> num[i];
13             tot+=num[i];
14         }
15         int ave=tot/n;
16         int tar[150];
17         fill(tar,tar+n,ave);
18         int sn=tot%n;
19         for(int i=n-1;i>=0&&sn;--i){
20             num[i++];sn--;
21         }
22         int mmin=0;
23         for(int i=0;i<n;++i)
24             if(tar[i]<num[i])    mmin+=num[i]-tar[i];
25         cout << "Set #" << t++ << endl;
26         cout << "The minimum number of moves is " << mmin << "." << endl;
27         cout << endl;
28     }
29 }
时间: 2024-10-23 00:06:29

HDU 1326的相关文章

zju 1251 Bricks box 砖盒----------hdu 1326

#include <iostream> using namespace std; int main() { int a[51],n,k=1; while(cin>>n,n) { int i,ans=0,s=0; for(i=0;i<n;i++) { cin>>a[i]; s+=a[i]; } s/=n; for(i=0;i<n;i++) if(a[i]>s)ans+=a[i]-s; cout<<"Set #"<&l

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到平均墙的高度(即最后墙的高度),遍历所有墙,如果小于平均墙,则用平均墙减去高度即是要移动的高度,统计所有需要“补”的高度即可.注意输出. AC Code: 1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int

HDU 1326 Box of Bricks(简单数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 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 siste

(HDU)1326 -- Box of Bricks (砖块)

题目链接:http://vjudge.net/problem/HDU-1326 一堆砖要摆平,问最少移动多少块.刚开始认为是最少要移动多少次,一个水题被写成了贪心题.mdzz. 注意输出还有一个换行!! 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <str

杭电 HDU ACM 1326 Box of Bricks

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5457    Accepted Submission(s): 2358 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

HDOJ 1326. Box of Bricks 纯水题

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5994    Accepted Submission(s): 2599 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201