# POJ 1877 Flooded!（二分）

Description

Input

Output

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.
Solution

Code

#include <iostream>
#include <cmath>
using namespace std;
#define MAXN 35
#define INF 9999999
#define esp 1e-12
int R,C,mCount=1,num;
double h[MAXN][MAXN],v,wl,per,low,high,mid,mv;
double min(double a,double b)//比较函数
{
return a<b?a:b;
}
int main()
{
int r,c;
while(scanf("%d%d",&R,&C)&&(R+C))
{
low=INF;//初始化
for(r=0;r<R;r++)
for(c=0;c<C;c++)
{
scanf("%lf",&h[r][c]);
low=min(low,h[r][c]);//更新二分下限
}
scanf("%lf",&v);
high=v/100.0+low;//二分上限
while(low+esp<high)//精度控制
{
num=mv=0;//初始化,num为被水淹没方块数量,mv为按此高度淹没方块所需水量
mid=(low+high)/2.0;
for(r=0;r<R&&mv+esp<v;r++)
for(c=0;c<C&&mv+esp<v;c++)
if(mid>h[r][c]+esp)
{
num++;
mv+=(mid-h[r][c])*100.0;
}
if(mv+esp<v)//水量足够淹没方块,下限增
low=mid;
else//水量不够淹没方块,上限减
high=mid;
}
printf("Region %d\n",mCount++);//按格式输出
printf("Water level is %.2lf meters.\n",mid);//按格式输出
printf("%.2lf percent of the region is under water.\n\n",double(num)/double(R*C)*100.0);//按格式输出
}
return 0;
}

07-13 333

04-02 381

03-27 2659

01-17 562

11-13 969

02-25 1924

01-21 930

10-17 455

03-13 150

10-30 276