poj 3334 Connected Gheeves (Geometry + BInary Search)
题意是,给出两个尖形的相连的容器,要求向其中灌水。它们具有日常的物理属性,例如两个容器中水平面高度相同以及水高于容器顶部的时候就会溢出。开始的时候打算直接用几何的计算,求出精确值。后来发现,这样的计算实在是太麻烦了,实现起来有很多细节要注意。于是,后来就想到了用二分的方法,枚举水平面的高度,然后求直线切割容器得到的多边形的面积,因为这个面积会随着水平面的高度具有单调性。注意预先确定好二分的上下界即可。1y~
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath> using namespace std; const double EPS = 1e-;
const double FINF = 1e10;
const int N = ;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && sgn(y - a.y) < ;}
bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.x) == ;}
} ;
typedef Point Vec;
Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);} template<class T> T sqr(T x) { return x * x;}
inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);} double polyArea(Point *pt, int n) {
double ret = 0.0;
pt[n] = pt[];
for (int i = ; i < n; i++) ret += crossDet(Point(0.0, 0.0), pt[i], pt[i + ]);
return fabs(ret) / 2.0;
}
inline Point lineIntersect(Point P, Vec u, Point Q, Vec v) { return P + u * (crossDet(v, P - Q) / crossDet(u, v));} Point gheef[][N], tmp[N];
double bottom[];
int n[]; double getArea(double x, int id) {
if (x <= bottom[id]) return 0.0;
Point s = Point(-FINF, x), t = Point(FINF, x);
for (int i = ; i < n[id]; i++) tmp[i] = gheef[id][i];
int l = , r = n[id] - ;
while (crossDet(tmp[l], s, t) * crossDet(tmp[l + ], s, t) > ) l++;
while (crossDet(tmp[r], s, t) * crossDet(tmp[r - ], s, t) > ) r--;
tmp[l] = lineIntersect(s, t - s, tmp[l], tmp[l + ] - tmp[l]);
tmp[r] = lineIntersect(s, t - s, tmp[r], tmp[r - ] - tmp[r]);
return polyArea(tmp + l, r - l + );
} double getArea(double x) {
double ret = 0.0;
for (int i = ; i < ; i++) {
ret += getArea(x, i);
}
return ret;
} double dc2(double x) {
// cout << getArea(3.536) << endl;
double l = min(bottom[], bottom[]), r = min(min(gheef[][].y, gheef[][].y), min(gheef[][n[] - ].y, gheef[][n[] - ].y));
// cout << l << " l r " << r << endl;
while (l + 1e- < r) {
double m = (l + r) / 2.0;
if (getArea(m) > x) r = m;
else l = m;
}
return l;
} int main() {
// freopen("in", "r", stdin);
int T;
double v;
cin >> T;
while (T-- && cin >> v) {
for (int i = ; i < ; i++) {
cin >> n[i];
bottom[i] = FINF;
for (int j = ; j < n[i]; j++) {
cin >> gheef[i][j].x >> gheef[i][j].y;
bottom[i] = min(bottom[i], gheef[i][j].y);
}
}
printf("%.3f\n", dc2(v));
}
return ;
}
——written by Lyon
poj 3334 Connected Gheeves (Geometry + BInary Search)的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- 九章算法系列(#2 Binary Search)-课堂笔记
前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一 ...
- 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree
1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...
- Algo: Binary search
二分查找的基本写法: #include <vector> #include <iostream> int binarySearch(std::vector<int> ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
随机推荐
- XMLHttpRequest cannot load JSONP
转自CSDN竹音林 1.前端JS代码 <script type="text/javascript" > $.ajax({ dataType:"JSONP&qu ...
- JavaScript-JQ初探实现自定义滚动条
这是一个基本实现思路,如果有新手和我一样没什么事,喜欢瞎研究话,可以参考下. 一.Html <div class="scroll_con"> <div class ...
- hdu 4512 (LCIS)
好久没写解题报告了,最近几周好忙...感觉是我进大学以来最忙的一段时间了,要给新生准备下周三比赛的题目,下周五要去南京赛区,回来之后马上就要期中考试了...不想挂科 额,很早之前就看过关于LCIS ...
- CentOS8/RHEL8--恢复root用户密码及简易加固GRUB
CentOS8/RHEL8--简易加固GRUB 今天突然想到放在数据中心的虚拟化平台下的Linux服务器,都是采用默认方式安装的,没有设置太多的安全选项,如果有恶意用户重启服务器后,通过GRUB调整启 ...
- [运维]ESXI系统的安装 标签: 虚拟机运维vmware服务器虚拟化 2017-05-05 09:24 496人阅读 评论(15)
上篇博客说到了VMware vSphere,那么接下来就讲一下我们如何将之投入使用.vsphere的虚拟机管理程序就是esxi. 什么是ESXI? vSphere产品套件的核心产品是虚拟机管理程序,作 ...
- 在Liferay 7中如何自定义一个Portlet的toolbar
哈哈,懒得自己写了,直接贴教程了,你想为那个portlet添加自定义的toolbar,就在javax.portlet.name=属性中写上它的值.教程博客:Adding Portlet URL in ...
- nginx+tomcat集群+redis(memcache)session共享!
常用保持session的方式: 1.一些代理(比如nginxIP_hash) 1.使用数据库来存储Session 2.使用Cookie来存储Session ...
- Directx11教程(10) 画一个简易坐标轴
原文:Directx11教程(10) 画一个简易坐标轴 本篇教程中,我们将在三维场景中,画一个简易的坐标轴,分别用红.绿.蓝三种颜色表示x,y,z轴的正向坐标轴. 为此,我们要先建立一个A ...
- 【风马一族_php】NO3_php基础知识
原文来自:http://www.cnblogs.com/sows/p/6001079.html(博客园的)风马一族 侵犯版本,后果自负 回顾 PHP基础语法 1.标签:<?php //PHP 代 ...
- 跨域知识(一)——CORS
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...