【贪心】【模拟】[POJ1877]Flooded!

10 篇文章 0 订阅
7 篇文章 0 订阅

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米,现在往矩阵中注水,水先会淹没低的柱子而且不会流出矩阵,求最后水面的海拔和被完全淹没的柱子占总柱子数的百分比。

分析

当时看到这道题我觉得我可以把矩阵转化成一排柱子,并升序排列。然后一个一个的去注水模拟。因为考虑到有些情况水浸没了所有的柱子,而程序不能出结果,就虚拟一个柱子并把它设为最大值,而不把它计算在柱子的总个数内。
如果你输出超限了,就可能把语言交成G++了,当初我就这样被坑了五六次。%>_<%

源代码

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int m,n,t;
double a[901],w;
bool f;
int main()
{
    while(~scanf("%d%d",&m,&n)){
        if(!m&&!n) return 0;
        t++;
        a[m*n+1]=0x7fffffff;//虚拟一个柱子
        for(int i=1;i<=m*n;i++)
            scanf("%lf",&a[i]);
        scanf("%lf",&w);
        sort(a+1,a+m*n+1);//升序排序
        double s=100;
        for(int i=2;i<=m*n+1;i++){
            if(a[i]-a[i-1]<w/s){
                w-=(a[i]-a[i-1])*s;//剩余的水减少
                s+=100;//浸没的面积增加
            }
            else{
                printf("Region %d\n",t);
                printf("Water level is %.2lf meters.\n",a[i-1]+w/s);
                printf("%.2lf percent of the region is under water.\n",w==0?0.0:(i-1)*100.0/(m*n));
                break;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值