Fill the Cisterns!

题目链接:

http://acm.hust.edu.cn/vjudge/contest/129783#problem/F

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

Output


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

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


2016-HUST-线下组队赛-3


##题意:

给出n个长方体水箱,从下往上依次注入V升水. 求最后结果的高度.


##题解:

对长方体的上下底面排序后直接模拟即可,维护当前高度时长方体的截面面积之和. 每次枚举到下底时把面积加入,枚举到下底时减去面积.
当枚举到某个上底时,水不够注满到这个高度,那么用剩余体积除以当前截面,就是剩下的高度.
还有种做法是二分最终高度,并遍历所有长方体计算是否能够用.

这道水题成为了今天的败笔. 2个多小时才过.
一开始就想的是二分,然后我特意处理都乘了个100来避免浮点数. 结果WA. 应该是因为可能不能恰好用完导致的.
然后改成double的还是WA. 后来重写了一份模拟,还是WA.
错点在于,一开始把rounded up错误理解成为向上取整,结果每次输出我都加了个0.005导致GG.
模拟过了之后以为二分的精度不够,这题不能用二分. 回来补题才发现输出一定只能用 %f , 不能用 %lf. (否者WA,至于精度,各种姿势处理都能过).

double输出再用%lf就吃键盘!!!!!


##代码:
####模拟:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 50010
#define mod 100000007
#define inf 0x3f3f3f3f3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

LL V;

struct node {

LL b,h,w,d;

}p[maxn];

bool vis[maxn];

typedef pair<LL,int> pii;

pii edge[maxn*2];

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
scanf("%d", &n);
int cnt = 0;
for(int i=1; i<=n; i++) {
scanf("%I64d %I64d %I64d %I64d", &p[i].b, &p[i].h, &p[i].w, &p[i].d);
edge[++cnt] = make_pair(p[i].b, i);
edge[++cnt] = make_pair(p[i].b + p[i].h, i);
}
scanf("%I64d", &V); sort(edge+1, edge+1+2*n);
memset(vis, 0, sizeof(vis));
LL area = 0;
LL ans = edge[1].first;
double last = inf;
for(int i=1; i<=2*n; i++) {
LL top = edge[i].first;
int num = edge[i].second; if((top-ans)*area >= V) {
last = (double)V / (double)area;
V = 0;
break;
} if(vis[num]) {
V -= area * (top-ans);
area -= p[num].w*p[num].d;
ans = top;
} else {
V -= area * (top-ans);
area += p[num].w*p[num].d;
vis[num] = 1;
ans = top;
}
} if(V > 0) printf("OVERFLOW\n");
else {
printf("%.2f\n", last+(double)ans);
}
} return 0;

}

####二分:

include

include

include

include

include

include

include

include

include

include

include

define LL long long

define eps 1e-8

define maxn 50010

define mod 100000007

define inf 0x3f3f3f3f

define mid(a,b) ((a+b)>>1)

define IN freopen("in.txt","r",stdin);

using namespace std;

int n;

double V;

struct node {

double b,h,w,d;

bool operator < (const node& B) const {

return b < B.b;

}

}p[maxn];

double cal(double dep) {

double ret = V;

for(int i=1; i<=n && dep>=p[i].b; i++) {

if(dep >= p[i].b + p[i].h) ret -= p[i].h * p[i].w * p[i].d;

else ret -= (dep - p[i].b) * p[i].w * p[i].d;

if(ret < 0) break;

}

return ret;

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
scanf("%d", &n);
double L = inf, R = -inf;
for(int i=1; i<=n; i++) {
scanf("%lf %lf %lf %lf", &p[i].b, &p[i].h, &p[i].w, &p[i].d);
L = min(L, p[i].b);
R = max(R, p[i].b + p[i].h);
}
scanf("%lf", &V); double tmp = V;
for(int i=1; i<=n; i++) {
tmp -= p[i].h*p[i].w*p[i].d;
if(tmp < 0) break;
} if(tmp > 0) {
printf("OVERFLOW\n");
continue;
} sort(p+1, p+1+n); double mid;
double ans = inf;
while(L <= R) {
mid = (L + R) / 2.0;
double cur = cal(mid);
if(cur <= 0) {
if(fabs(cur) < eps) ans = min(ans, mid);
R = mid - 0.001;
}
else L = mid + 0.001;
}
ans = min(ans, mid); printf("%.2f\n", ans);
} return 0;

}

POJ 1434 Fill the Cisterns! (模拟 or 二分)的更多相关文章

  1. poj1434 Fill the Cisterns!

    地址:http://poj.org/problem?id=1434 题目:Fill the Cisterns! Fill the Cisterns! Time Limit: 5000MS   Memo ...

  2. POJ 2723 Get Luffy Out(2-SAT+二分答案)

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8851   Accepted: 3441 Des ...

  3. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  4. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

  5. Poj 3233 Matrix Power Series(矩阵二分快速幂)

    题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...

  6. POJ 1027 The Same Game(模拟)

    题目链接 题意 : 一个10×15的格子,有三种颜色的球,颜色相同且在同一片内的球叫做cluster(具体解释就是,两个球颜色相同且一个球可以通过上下左右到达另一个球,则这两个球属于同一个cluste ...

  7. poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16396   Accepted: 750 ...

  8. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

  9. POJ 3233 Matrix Power Series (矩阵+二分+二分)

    题目地址:http://poj.org/problem?id=3233 题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值 题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而 当k ...

随机推荐

  1. [Git] 016 远程仓库篇 第三话 删除远程仓库

    1. 来到自己的 GitHub 页面,先点右上角自己的头像,再点 "Your profile" 2. 选择自己的某个远程仓库,我选 "git_skills" 3 ...

  2. [转帖]使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单

    使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单 https://www.cnblogs.com/martinzhang/p/5348769.html good good ...

  3. MySQL-快速入门(9)视图

    1.什么是视图 视图是一个虚表.视图可以进行查询.增加.修改.删除.进行修改.增加.删除,将影响基本表中的数据. 2.视图相对基本表的优势 1>简单化:看到的就是想要的字段列,可以简化后续查询. ...

  4. tz汇报

    不爽,不满意,存在太多Bug,汇报前的了解不充分,了解到了有那些领导参加,但是没有具体了解领导的时间安排,没有按照领导的时间调整汇报提纲及思路,汇报到1个半小时,领导需要参加会议,提前离开,没能够与领 ...

  5. 通过QT查找Word中的关键字,并做高亮或删除操作

    最近由于项目需要,要用QT操作Word文档.具体的工作需求:在指定的Word文档(*.doc文件/*.docx文件)中查找关键字,找到后做高亮操作或者直接删除操作,然后另存为到别的目录(表示这个文件被 ...

  6. java中的四种引用方式(强引用,软引用,弱引用,虚引用)

    java内存管理主要有内存分配和内存回收,都不需要程序员负责,垃圾回收的机制主要是看对象是否有引用指向该对象. java中对象的引用主要有四种:强引用,软引用,弱引用,虚引用. Java中提供这四种引 ...

  7. 《CSS权威指南》双鱼书概述——第一章CSS和文档

    前言:CSS即层叠样式表 主要是影响一个或者一组文档的表现,没有文档,CSS毫无用处. 一.web的衰落 完全结构化的文本太过古板,太平常,虽然有N个理由要求使用结构化标记,但HTML已然走到了今天. ...

  8. react 从商品详情页返回到商品列表页,列表自动滚动上次浏览的位置

    现状:目前从商品详情页返回到商品列表页,还需要再去请求服务数据,还需要用户再去等待获取数据的过程,这样用户体验非常不好, 遇到的问题: 1:如何将数据缓存, 2:如何获取和保存列表滑动的高度, 3:判 ...

  9. drop,delete,truncate 的区别

    (1)DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作. TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独 ...

  10. 牛客练习赛14 E - 无向图中的最短距离 (bfs+bitset)

    一个链接:https://ac.nowcoder.com/acm/contest/82/E来源:牛客网 无向图中的最短距离 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144 ...