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. 先对所有海拔排序,从小到大
然后,选取中间的海拔作为最终填满T1体积水后的海拔高度,判断其与T的关系,若T1>T,选取海拔较小的海拔,填满T2体积水。
直到得到两相邻海拔高度的T1,T2,使得(T1-T)*(T2-T)<0,则说明最后的水的高度在T1和T2之间。
考虑到数据个能比较多,采用二分法做该题。
但是,最后一直 结果错误,以下为采用二分法的错误代码
情况很多,细节也很多。
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <iomanip>
using namespace std;
const int maxn = *+;
int s[maxn], sum[maxn],m,n; //s为各个网格的海拔高度,有正有负 void dataout(double a1,double a2,double ss,double numm,double T)
{
//cout<<a1<<" "<<a2<< " "<<ss<< " "<<numm<<endl;
if(numm == )numm=;
double height = ss + (a1 - a2) / numm , per;
if(T == )per=;
else per = * numm / (m*n) ;
cout<<setiosflags(ios::fixed)<<setprecision();
cout << "Water level is " << height << " meters."<<endl;
cout << per << " percent of the region is under water."<<endl;
} void deal2(double T)
{
sort(s, s + m * n);
int a = , b = n * m - ,mid; //用二分法从中间向两边计算
while(){
mid = (a + b) / ;
if(sum[mid]!=)sum[mid]=;
for(int i = ;i < mid; i++){
if(s[i] < s [mid]) {
sum[mid] += (s[mid] - s[i]);
}
}
if (sum[mid] < T) a = mid;
else b = mid;
if(abs(a-b)<=)break;
}
if((sum[a-]-T)*(sum[a]-T) < &&a!=){
if(sum[a-] > T) dataout(T,sum[a],s[a],a-,T);
else dataout(T,sum[a-],s[a-],a,T);
}
else{
if(sum[a+] > T) dataout(T,sum[a],s[a],a+,T);
else dataout(T,sum[a+],s[a+],a,T);
}
} void deal1(double T)
{
sort(s, s + m * n);
int i;
bool flag = false;
sum[] = ;
for(i=;i<n*m;i++){
for(int j=;j<i;j++){
sum[i]+=(s[i] - s[j]);
/*cout<<"i "<<i<<" j "<<j<<endl;
cout<<sum[i]<<" = "<<s[i]<<" - "<<s[j]<<endl;*/
}
if(sum[i] >= T){
flag = true;
break;
}
}
/*cout<<i<<endl;
cout<<"sum[i-1] "<<sum[0]<<" sum[i] "<<sum[1]<<endl;*/
if(flag){
double height ,per;
if(T == ){
per=;
height = s[];
}
else {
per = * i / m / n;
height = s[i-] + (sum[i] - sum[i-]) / i;
}
cout<<setiosflags(ios::fixed)<<setprecision();
cout << "Water level is " <<height << " meters."<<endl;
cout << per<<" percent of the region is under water."<<endl;
}
else{
i--;
/*cout<<"false"<<endl;
cout<<"i "<<i<<endl;
cout<<" s[i-1] "<<s[i-1]<<" sum[i] "<<sum[i]<<endl;*/
double height ,per;
if(T == ){
per = ;
height = s[];
}
else {
per = ;
height = s[i] + (T - sum[i]) / (i + ) ;
}
cout<<setiosflags(ios::fixed)<<setprecision();
cout << "Water level is " <<height << " meters."<<endl;
cout << per<<" percent of the region is under water."<<endl;
}
} int main()
{
int times=;
double T;
while( cin >> n >> m ){
if(n == )break;
for(int i = ;i < n*m;i++)cin >> s[i];
cin >> T;
cout<< "Region " << ++times << endl;
memset(sum,,sizeof(sum));
if(m == &&n == ){
double height = T/ + s[],per;
if(T == ) per = ;
else per = ;
cout << setiosflags(ios::fixed) << setprecision();
cout << "Water level is " <<height << " meters."<<endl;
cout << per << " percent of the region is under water."<<endl;
}
else if(m*n<)deal1(T/);
else deal2(T / );
}
return ;
}
实在不知错在哪,就不用二分法试试。
一次AC
果然还是我想太多了,不用二分法也并没有超时。
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
const int maxn = *+;
int s[maxn],m,n; //s为各个网格的海拔高度,有正有负 void dataout(double a1,double a2,double ss,double numm,double T)
{
if(numm == )numm=;
double height = ss + (a1 - a2) / numm , per = * numm / (m*n);
cout<<setiosflags(ios::fixed)<<setprecision();
cout << "Water level is " << height << " meters."<<endl;
cout << per << " percent of the region is under water."<<endl;
} void deal(double T)
{ int a = ,t1 = ,t2 = ,i;
for(i=;i<n*m;i++){
t2 = t1;
t1 = ;
for(int j=;j<i;j++)t1+=(s[i] - s[j]);
if(t2< T&& (t1 > T||t1 == T))break; }
if(i == n*m) dataout(T,t1,s[i-],i,T);
else dataout(T,t2,s[i-],i,T);
} int main()
{
int times=;
double T;
while( cin >> n >> m ){
if(n == )break;
for(int i = ;i < n*m;i++)cin >> s[i];
cin >> T;
cout<< "Region " << ++times << endl;
sort(s, s + m * n);
if(T == ){
double height = s[], per = ;
cout << setiosflags(ios::fixed) << setprecision();
cout << "Water level is " <<height << " meters."<<endl;
cout << per << " percent of the region is under water."<<endl;
}
else deal( T / );
}
return ;
}



uvA Flooded!的更多相关文章

  1. UVA 815 Flooded!

    题意:来自:https://blog.csdn.net/lecholin/article/details/70186673 思路: ①数组存每个网格的高度,然后排序,做题时想象为上面的柱状图. ②注意 ...

  2. 【每日一题】Flooded! UVA - 815 模拟阅读格式题

    https://cn.vjudge.net/problem/UVA-815 题意:给你一个矩阵,每个格子的数代表一个海拔并且每个格子的面积100平方米.给你整个区域的降水量(立方米),问降水量(米). ...

  3. 【习题 4-9 UVA - 815】Flooded!

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 题目很迷啊. 不会出现盆地? 可以理解为一条线. 从左往右高度上升的一座座山. 然后V升的水从最左边的山倒进去. 然后问你最后海拔多 ...

  4. Flooded! UVA - 815 (sort排序)

    错了好多遍,不知道为啥出错,如果有大神发现,请求指点!!! 附错误代码(错的不知道怎么回事): #include<iostream> #include<cstdio> #inc ...

  5. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  6. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  7. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  8. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  9. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

随机推荐

  1. C#窗体嵌套

    1.思路:在一个面板上显示或者隐藏不同窗体 private void button1_Click(object sender, EventArgs e) { chuangti at = new chu ...

  2. 在java中生成二维码,并直接输出到jsp页面

    在java中生成的二维码不存到磁盘里要直接输出到页面上,这就需要把生成的二维码直接以流的形式输出到页面上,我用的是myeclipse 和 tomcat 它的原理是:在加载页面时,根据img的src(c ...

  3. Maven基础教程

    更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...

  4. jquery 自动实现autocomplete+ajax

    来公司也差不多一个半月了,一直做点小东西,现在在做公司的出货系统,也只是做来锻炼锻炼的. 好了 不废话了 下面是实现 jquery插件 autocomplete+ajax 自动实现.也是刚学,勿喷. ...

  5. 火狐的bug

    初次启动火狐的界面并且默认是最大化的情况下,第一个业签时会发现火狐的浏览器无法达到下边框,请看图 途中可以看到,body区域没有填充满浏览器可用区域.但是当浏览器已经启动页签,现在是第二个页签时,则不 ...

  6. CSS3 transition 动画过度属性

    <!DOCTYPE html> <html> <head> <style>  div { width:100px; height:100px; back ...

  7. Service Lane

    Link https://www.hackerrank.com/challenges/service-lane def main(): n, t = map(int, raw_input().spli ...

  8. Unity有限状态机编写

    有限状态机FSM 是对行为逻辑的抽象. 在整个FSM架构中 首先有一个状态基类stateObject 里面有三个方法,分别是状态前.状态中.状态后. 所有具体行为类都要继承这个基类,在这三个方法中具体 ...

  9. Effective Java2读书笔记-类和接口(四)

    第19条:接口只用于定义类型 这一条就举了一个反例,说有些接口中只包含常量.这是对接口的不良使用.要实现相同的功能,应该使用不可实例化的工具类(第4条说过). public class Physica ...

  10. 【转】Linux中history历史命令使用方法详解

    原文网址:http://os.51cto.com/art/201205/335040.htm 当你在玩Linux的时候,如果你经常使用命令行来控制你的Linux系统,那么有效地使用命令历史机制将会使效 ...