UVa815_Flooded!

 1 #include <iostream>
 2 //#include <fstream>
 3 #include <iomanip>
 4 #include <cstdio>
 5 #include <algorithm>
 6 #include <cstring>
 7 using namespace std;
 8
 9 const int maxn = 30 * 30 + 10;
10 const int space = 10 * 10;
11
12 int main()
13 {
14     //ofstream out("out.txt");
15     int m, n, kase = 0, volume;    //降水总量
16     double eleva[maxn], height;    //每个格子的海拔高度, 输入的高度
17     double percnt = 0;             //有多少百分比的区域
18     while (cin >> m >> n)
19     {
20         memset(eleva, 0, sizeof(eleva));
21         if (!m && !n) break;
22         for (int i = 0; i < m; i++)
23         {
24             for (int j = 0; j < n; j++)
25             {
26                 cin >> height;
27                 eleva[i * n + j] = height;
28             }
29         }
30         cin >> volume;
31         sort(eleva, eleva + m*n);              //排序
32
33         int k, comput = 0;                     //计算已经淹没地区的水量
34         for (k = 1; k < m*n; k++)
35         {
36             int tmp = comput + (eleva[k] - eleva[k - 1]) * space * k;
37             if (tmp >= volume)
38                 break;
39             comput = tmp;
40         }
41         double water = (volume - comput) / (double)(k * space) + eleva[k - 1];    //最终水平面高度是 = 当前被淹没的最高海拔 + 水面比当前海拔高出的高度
42
43         cout << "Region " << ++kase << "\n";
44         cout << fixed << setprecision(2)
45              << "Water level is " << water << " meters.\n";
46
47         double total = m*n;
48         percnt = (k*100.0 / total) ;         //被淹没区域比例 = 当前比水平面低的海拔数 / 区域总数
49
50         cout << fixed << setprecision(2)
51             << percnt << " percent of the region is under water.\n\n";
52     }
53
54     return 0;
55 }

如图,将海拔按高矮排序好(先排序好,比较容易计算已经淹没地区的水量),由于是水往地处流,如果水量如图,淹没区域如图中阴影地区。

时间: 2024-11-05 18:47:26

UVa815_Flooded!的相关文章