【模拟】【贪心】POJ1877Flooded!

来源:互联网 发布:python xpath解析html 编辑:程序博客网 时间:2024/05/16 02:53

题目

Flooded!
Time Limit: 1000MS Memory Limit: 30000K
Special Judge

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 low-lying 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.

题目大意

一个由很多柱子组成的矩阵,每个柱子的底面积为100平方米,高度为海拔(可以为正,也可以为负)x米,现在往矩阵中注水(水会先到达最矮的柱子,再淹第二矮的……),水不会流出矩阵,求最后水面的海拔和被完全淹没(刚好到达不算)的柱子占总柱子数的百分比。

思路

原本看的翻译中没有“水会先到达最矮的柱子,再淹第二矮的……”这一句,事实上原题中有说,所以就想了很久没有思路……(其实不说也是这样的= =),所以以后还是看原题吧。

既然先淹矮的,那就排序呗,将柱子高度存在一个一维数组过后从小到大排序,然后从最矮的开始硬模拟注水,至于怎么模拟,自己想吧……我也不好说,详见代码。

还有一个细节,柱子的海拔有可能为负,反正我处理了:把所有柱子的海拔减去最低的柱子的海拔。

代码

#include<cstdio>#include<algorithm>using namespace std;#define MAXN 1000int h[MAXN+5];int main(){    int cas=0;    while(1)    {        int R,C,N,minx=0x7fffffff,wt;        scanf("%d%d",&R,&C);        if(!R&&!C) return 0;        N=R*C;//柱子个数        for(int i=1;i<=N;i++)        {            scanf("%d",&h[i]);            minx=min(minx,h[i]);//求最矮的柱子        }        scanf("%d",&wt);        sort(h+1,h+N+1);//排序        for(int i=1;i<=N;i++) h[i]-=minx;//处理负数,即将最矮的高度降为0,后面的高度为与最矮高度的相对高度        double wh=minx,bm=100,ans=0;//wh一开始为最矮柱子的高度,bm为当前的底面积(每淹过一个柱子底面积就要变大)        h[N+1]=0x7fffffff;//因为水流不出矩阵,所以最后一个柱子无限高        for(int i=1;i<=N;i++,ans++,bm+=100)//一个一个柱子淹        {            if(wt<=bm*(h[i+1]-h[i]))//水淹不过下一个柱子            {                wh+=wt/bm;//加上水能淹的高度并跳出循环                break;            }            else            {                wh+=h[i+1]-h[i];//直接加上柱子高度之差                wt-=bm*(h[i+1]-h[i]);//水的体积减少            }        }        printf("Region %d\n",++cas);        printf("Water level is %.2lf meters.\n",wh);        printf("%.2lf percent of the region is under water.\n",(ans+1)*100/N);//ans要+1,因为最后跳出时的柱子也被淹了    }}