Files
python/RuMenJingDian/UVa/UVa815.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
//UVa 815 Flooding!(排序)
//https://blog.csdn.net/qq_27474589/article/details/75416171
using namespace std;
int a[1000];
int main() {
int m, n, cubi, cnt = 0;
while (~scanf("%d%d", &m, &n) && m && n) {
for (int i = 0; i < m * n; i++) scanf("%d", &a[i]);
scanf("%d", &cubi);
double cub = cubi * 1.0 / 100;
sort(a, a + m * n);
int sum = 0, i = 0, last = 0;
double high, percent;
for (i = 1; i < m * n && (cub - sum) > 0; i++) {
last = sum;
sum += i * (a[i] - a[i - 1]);
}
//高度为零,还需要特判
if (cub == 0) {
high = a[0];
percent = 0;
} else if (i == m * n && (cub - sum) > 0) {//水填满了最高海拔的格子,还有多的水
high = (cub - sum) * 1.0 / i + a[i - 1]; //平分这些水
percent = 100; //100%覆盖
} else {
/*水没有填满最高海拔的格子,在中间时就不够用了。
这里用到sum的上一次结果所以上面for循环中用了last保存。*/
high = (cub - last) * 1.0 / (i - 1) + a[i - 2];
percent = (i - 1) * 1.0 / (m * n) * 100;
}
printf("Region %d\n", ++cnt);
printf("Water level is %.2f meters.\n", high);
printf("%.2f percent of the region is under water.\n\n", percent);//注意空行在最后面
}
return 0;
}