poj 1434 --Fill the Cisterns!(计算几何,二分)

该博客介绍了一道POJ编程题,题目要求根据水池的尺寸和位置,计算在给定量的水中,水池能被填满到的高度。解题策略是采用二分查找方法,确定有效区间并检查是否存在满足条件的划分线。需要注意的是,在二分过程中,即使找到的线段满足条件,也需要考虑是否确实穿过水池,且结果的精度要求控制在0.001以内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=1434


Fill the Cisterns!
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 1806 Accepted: 653

Description

During the next century certain regions on earth will experience severe water shortages. The old town of Uqbar has already started to prepare itself for the worst. Recently they created a network of pipes connecting the cisterns that distribute water in each neighbourhood, making it easier to fill them at once from a single source of water. But in case of water shortage the cisterns above a certain level will be empty since the water will to the cisterns below.



You have been asked to write a program to compute the level to which cisterns will be lled with a certain volume of water, given the dimensions and position of each cistern. To simplify we will neglect the volume of water in the pipes.


Task

Write a program which for each data set:

reads the description of cisterns and the volume of water,

computes the level to which the cisterns will be filled with the given amount of water,

writes the result.

Input

The first line of the input contains the number of data sets k, 1 <= k <= 30. The data sets follow.

The first line of each data set contains one integer n, the number of cisterns, 1 <= n <= 50 000. Each of the following n lines consists of 4 nonnegative integers, separated by single spaces: b, h, w, d - the base level of the cistern, its height, width and depth in meters, respectively. The integers satisfy 0 <= b <= 10^6 and 1 <= h * w * d <= 40 000. The last line of the data set contains an integer V - the volume of water in cubic meters to be injected into the network. Integer V satisfies 1 <= V <= 2 * 10^9.

Output

The output should consist of exactly d lines, one line for each data set.

Line i, 1 <= i <= d, should contain the level that the water will reach, in meters, rounded up to two fractional digits, or the word 'OVERFLOW', if the volume of water exceeds the total capacity of the cisterns.

Sample Input

3
2
0 1 1 1
2 1 1 1
1
4
11 7 5 1
15 6 2 2
5 8 5 1
19 4 8 1
132
4
11 7 5 1
15 6 2 2
5 8 5 1
19 4 8 1
78

Sample Output

1.00
OVERFLOW
17.00



题意:给出N个水池,水池放置的高低不同,容量尺寸也不同,现在给你一定的水量V,求把这些水倒到这些水池后水涨到的高度。

解法:二分。

1.先找出水池的有效区间,即放在最下面的水池的底部LOW,和水池中顶部最高的高度HIGH,即为将要进行二分的区间。

2.二分区间确定一条线,测试这条线是否刚好能让给的水量V涨到这个高度。

3.即使得到的划分线MID能够符合在这条线以下的水量刚好就是V,它也不一定就是正解。

因为有可能这条线没有划过任何水池的中间部分。如例1,当划的线为1.5时,也能满足这个条件。

所以这种情况下必须把这条线当作HIGH继续二分查找。


另外精度控制必须<0.001,我尝试0.002WA了。


代码:

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
#define min(a,b) a<b?a:b
struct cistren
{
  int b,h,w,d,f;
}num[50010];
int sum,n;

double cal(double &line)
{
  double result=0;
  for(int i=0;i<n;i++)
    {
      if(line>num[i].b)
	{
	  double t=min(line,num[i].f);
	  result+=(t-num[i].b)*num[i].w*num[i].d;
	}
    }
  return result;
}
int main()
{
  int t,i;
  scanf("%d",&t);
  while(t--)
    {
      scanf("%d",&n);
      int total=0;
      double low=99999999,high=0;
      for(i=0;i<n;i++)
	{
	  scanf("%d%d%d%d",&num[i].b,&num[i].h,&num[i].w,&num[i].d);
	  num[i].f=num[i].b+num[i].h;
	  total+=num[i].h*num[i].w*num[i].d;
	  if(num[i].b+num[i].h>high)
	    high=num[i].b+num[i].h;
	  if(num[i].b<low)
	    low=num[i].b;
	}
      scanf("%d",∑);
      if(sum>total)
	{
	  printf("OVERFLOW\n");
	}
      else 
	{
	  double mid;
	  while(high-low>0.001)
	    {
	      mid=(low+high)/2.0;
	      double ans=cal(mid);
	      if(fabs(ans-sum)<=0.001)
		{
		  for(i=0;i<n;i++)
		    {
		      if(mid>num[i].b&&num[i].f>mid)
			break;
		    }
		  if(i<n)
		  break;
		  else
		    high=mid;
		}
	      else if(ans>sum)
		high=mid;
	      else
		low=mid;
	    }
	  printf("%.2lf\n",mid);
	}
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值