【原创】【贪心优化暴力模拟】Flooded! POJ 1877

Flooded! POJ 1877

题目

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.

题意

给你一个r*c的方格,每个方格的面积都是100㎡,给定高度(可正可负),再给你Vm³水,叫你去淹没这个方格,问最终水面高度以及有放给被完全淹没的百分比。

分析

题目描述已经说了,在高处的水会流向低处。因此我们可以直接往最低的方格里灌水。
首先我们要把这个方格“看扁”,把它压成一个一维数组,因为这样我们就可以sort了!
假设排序后的方格是这样的:
p1

假设该灌i号了:
p2

那么只会有两个情况:
情况一:全部灌溉了:
p3
此时损失的水为(h[i+1]-h[i])*i.而增加的高度为h[i+1]-h[i]
既然会损失水,所以就会有第二种情况:
p4
也就是水不够了,当然此时损失了所有的水,增加的高度为V(此时水的体积)/(i*100)(注意每个块块是100㎡)

代码差不多可以写出来了。

代码

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

void Read(int &p)
{
    p=0;
    int f=1;
    char c=getchar();
    while(c<'0'||c>'9') 
        f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9')
        p=p*10+c-'0',c=getchar();
    p*=f;
}

int r,c,a[1000],x,n,cnt,sp;
int main()
{
    while(1)
    {
        Read(r); Read(c); n=r*c;
        if(!r&&!c) break;
        for(int i=0;i<n;i++)
            Read(a[i]),a[i]*=100;
        sort(a,a+n);
        Read(x);

        double V=x*1.0;
        double height=a[0];
        cnt=0;
        cnt=1;
        for(int i=1;i<n;i++)
        {
            if((a[i]-a[i-1])*i<V)
            {
                V-=i*(a[i]-a[i-1]);
                cnt++; height=a[i];
            }
            else 
            {
                height+=V/i;
                V=0;
                break;
            }
        }
        if(V)  height+=V/n*1.0;
        printf("Region %d\n",++sp);
        printf("Water level is %.2lf meters.\n",height/100);
        printf("%.2lf percent of the region is under water.\n",cnt*100.0/n);
        puts("");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值