1999 ICPC WF H Flooded! 【思维】【细节】

来源:互联网 发布:php数组是什么数据结构 编辑:程序博客网 时间:2024/05/21 10:00

题目链接:
UVA-815

Description:
To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients with the elevation of each 10-meter by 10-meter square of land in regions where homes may be purchased. Water from rain, melting snow, and burst water mains will collect first in those squares with the lowest elevations, since water from squares of higher elevation will run downhill. For simplicity, we also assume that storm sewers enable water from high-elevation squares in valleys (completely enclosed by still higher elevation squares) to drain to lower elevation squares, and that water will not be absorbed by the land.
From weather data archives, we know the typical volume of water that collects in a region. As prospective homebuyers, we wish to know the elevation of the water after it has collected in lowlying squares, and also the percentage of the region’s area that is completely submerged (that is, the percentage of 10-meter squares whose elevation is strictly less than the water level). You are to write the program that provides these results.

Input
The input consists of a sequence of region descriptions. Each begins with a pair of integers, m and n, each less than 30, giving the dimensions of the rectangular region in 10-meter units. Immediately following are m lines of n integers giving the elevations of the squares in row-major order. Elevations are given in meters, with positive and negative numbers representing elevations above and below sea level, respectively. The final value in each region description is an integer that indicates the number of cubic meters of water that will collect in the region. A pair of zeroes follows the description of the last region.

Output
For each region, display the region number (1, 2, …), the water level (in meters above or below sea level) and the percentage of the region’s area under water, each on a separate line. The water level and percentage of the region’s area under water are to be displayed accurate to two fractional digits. Follow the output for each region with a blank line.

Sample Input
3 3
25 37 45
51 12 34
94 83 27
10000
0 0

Sample Output
Region 1 Water level is 46.67 meters.
66.67 percent of the region is under water.

题目大意:
有一个n*m(1≤m,n<30)的网格,每个格子是边长10米的正方形,网格四周是无限大的墙壁。输入每个格子的海拔高度,以及网格内雨水的总体积,输出水位的海拔高度以及有多少百分比的区域有水(即高度严格小于水平面)。

解题思路:
因为雨水始终是从高处往低处流淌的,所以水位的海拔高度只与每个格子的高度有关,而与格子的布局无关。为了方便模拟雨水的上升过程,我们将所有格子按照从低到高的顺序排在一条直线上,依次加水,直到水位高于下一格子,再以下一格子为基准加水……这样最终会出现两种情况:第一,水位高于最高的格子;第二,水位位于其中俩个格子之间。然后分类讨论就好了。出现后者的情况时我用二分求解的最终结果。

Mycode:

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAX = 1005;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const double eps = 1e-8;double a[MAX];int main(){    int n, m;    int cas = 0, tot, num;    double wat, res, tem, ans;    while(cin >> n >> m && m)    {        tot = 0;        n *= m;        for(int i = 0; i < n; ++i)            cin >> a[i];        sort(a, a+n);        cin >> wat;        wat /= 100;        num = -1;        tem = 0;        for(int i = 1; i < n; ++i)        {            tem += (a[i] - a[i-1])*i;            if(tem >= wat)            {                num = i;                break;            }        }        if(num == -1) //全部淹没        {            ans = a[n-1] + (wat - tem)/n;            tot = n;        }        else        {            tem -= (a[num]-a[num-1])*num;            double l = a[num-1], r = a[num];            double hh = l;            while(r-l > eps)            {                double mid = (r+l)/2;                if(tem + (mid-hh)*num > wat)                    r = mid;                else                    l = mid;            }            ans = l;            for(int i = 0; i < n; ++i)            {                if(a[i] < ans) tot++;            }        }        res = 100.0 * tot / n;        printf("Region %d\n",++cas);        printf("Water level is %.2f meters.\n",ans);        printf("%.2f percent of the region is under water.\n",res);        puts("");    }    return 0;}
原创粉丝点击