POJ1434-Fill the Cisterns!

Fill the Cisterns!
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 3197 Accepted: 1080

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

Source

//AC代码
/*
题意:有n个水箱,高度不一样,现在有V的水倒入其中(从下往上),问你水最多能升多高
此题直接从下往上用扫描线就能A了,不过时间不是很好4700MS++
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=50001;
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
struct Cisterns
{
    int b;
    int h;
    int w;
    int d;
    int sum;
    Cisterns(const int &_b=0,const int &_h=0,const int &_w=0,const int &_d=0,const int &_sum=0):b(_b),h(_h),w(_w),d(_d),sum(_sum){}
    void input()
    {
        cin>>b>>h>>w>>d;
    }
    void output()
    {
        cout<<b<<" "<<h<<" "<<w<<" "<<d<<endl;
    }
}cs[Max];
struct Node
{
    int h;
    int f;
}node[Max];
bool cmp(Node u,Node v)
{
    return u.h<v.h;
}
int s[Max];
int main()
{
    int k,n,m,i,j;
    int res;
    double Total_V;
    double Total_H;
    int sum;
    cin>>k;
    while(k--)
    {
        cin>>n;
        res=0;
        m=0;
        for(i=1;i<=n;i++)
        {
            cs[i].input();
            cs[i].sum=cs[i].h*cs[i].w*cs[i].d;
            s[i]=cs[i].w*cs[i].d;
            node[m].h=cs[i].b;
            node[m++].f=i;
            node[m].h=cs[i].b+cs[i].h;
            node[m++].f=-i;
            res+=cs[i].sum;
        }
        cin>>Total_V;
        if(Total_V>res)
            cout<<"OVERFLOW"<<endl;
        else
        {
            sort(node,node+m,cmp);
            Total_H=node[0].h;
            sum=0;
            for(i=0;i<m;i++)
            {
                if(i>0&&node[i].h>node[i-1].h)
                {
                    Total_V-=sum*(node[i].h-node[i-1].h);
                    //cout<<Total_V<<endl;
                    Total_H+=node[i].h-node[i-1].h;
                    if(Total_V<=0)//如果水倒完了
                    {
                        Total_H+=Total_V/sum;//因为Total_V是负的(是空的那一部分),所以相当于减去空去高度
                        break;
                    }
                }
                if(node[i].f<0)//这里是加上重叠在一起的底面积
                    sum-=s[-node[i].f];
                else
                    sum+=s[node[i].f];
                    //cout<<sum<<endl;
            }
            cout<<setprecision(2)<<setiosflags(ios::fixed)<<Total_H<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值