3334 -- Connected Gheeves

  题意是,给出两个尖形的相连的容器,要求向其中灌水。它们具有日常的物理属性,例如两个容器中水平面高度相同以及水高于容器顶部的时候就会溢出。开始的时候打算直接用几何的计算,求出精确值。后来发现,这样的计算实在是太麻烦了,实现起来有很多细节要注意。于是,后来就想到了用二分的方法,枚举水平面的高度,然后求直线切割容器得到的多边形的面积,因为这个面积会随着水平面的高度具有单调性。注意预先确定好二分的上下界即可。1y~

代码如下:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. const double EPS = 1e-;
  10. const double FINF = 1e10;
  11. const int N = ;
  12. inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
  13. struct Point {
  14. double x, y;
  15. Point() {}
  16. Point(double x, double y) : x(x), y(y) {}
  17. bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && sgn(y - a.y) < ;}
  18. bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.x) == ;}
  19. } ;
  20. typedef Point Vec;
  21. Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
  22. Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
  23. Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
  24. Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);}
  25.  
  26. template<class T> T sqr(T x) { return x * x;}
  27. inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
  28. inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
  29. inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
  30. inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
  31.  
  32. double polyArea(Point *pt, int n) {
  33. double ret = 0.0;
  34. pt[n] = pt[];
  35. for (int i = ; i < n; i++) ret += crossDet(Point(0.0, 0.0), pt[i], pt[i + ]);
  36. return fabs(ret) / 2.0;
  37. }
  38. inline Point lineIntersect(Point P, Vec u, Point Q, Vec v) { return P + u * (crossDet(v, P - Q) / crossDet(u, v));}
  39.  
  40. Point gheef[][N], tmp[N];
  41. double bottom[];
  42. int n[];
  43.  
  44. double getArea(double x, int id) {
  45. if (x <= bottom[id]) return 0.0;
  46. Point s = Point(-FINF, x), t = Point(FINF, x);
  47. for (int i = ; i < n[id]; i++) tmp[i] = gheef[id][i];
  48. int l = , r = n[id] - ;
  49. while (crossDet(tmp[l], s, t) * crossDet(tmp[l + ], s, t) > ) l++;
  50. while (crossDet(tmp[r], s, t) * crossDet(tmp[r - ], s, t) > ) r--;
  51. tmp[l] = lineIntersect(s, t - s, tmp[l], tmp[l + ] - tmp[l]);
  52. tmp[r] = lineIntersect(s, t - s, tmp[r], tmp[r - ] - tmp[r]);
  53. return polyArea(tmp + l, r - l + );
  54. }
  55.  
  56. double getArea(double x) {
  57. double ret = 0.0;
  58. for (int i = ; i < ; i++) {
  59. ret += getArea(x, i);
  60. }
  61. return ret;
  62. }
  63.  
  64. double dc2(double x) {
  65. // cout << getArea(3.536) << endl;
  66. double l = min(bottom[], bottom[]), r = min(min(gheef[][].y, gheef[][].y), min(gheef[][n[] - ].y, gheef[][n[] - ].y));
  67. // cout << l << " l r " << r << endl;
  68. while (l + 1e- < r) {
  69. double m = (l + r) / 2.0;
  70. if (getArea(m) > x) r = m;
  71. else l = m;
  72. }
  73. return l;
  74. }
  75.  
  76. int main() {
  77. // freopen("in", "r", stdin);
  78. int T;
  79. double v;
  80. cin >> T;
  81. while (T-- && cin >> v) {
  82. for (int i = ; i < ; i++) {
  83. cin >> n[i];
  84. bottom[i] = FINF;
  85. for (int j = ; j < n[i]; j++) {
  86. cin >> gheef[i][j].x >> gheef[i][j].y;
  87. bottom[i] = min(bottom[i], gheef[i][j].y);
  88. }
  89. }
  90. printf("%.3f\n", dc2(v));
  91. }
  92. return ;
  93. }

——written by Lyon

poj 3334 Connected Gheeves (Geometry + BInary Search)的更多相关文章

  1. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  2. 九章算法系列(#2 Binary Search)-课堂笔记

    前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一 ...

  3. 将百分制转换为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 ...

  4. Algo: Binary search

    二分查找的基本写法: #include <vector> #include <iostream> int binarySearch(std::vector<int> ...

  5. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  6. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  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 ...

随机推荐

  1. Linux 下安装和使用 Redis

    系统: CentOS-7-1611 首先安装gcc 因为要make编译 参照官网 wget  下载包 解压  make 编译完成后会在src目录下生成几个可执行文件 然后执行:make install ...

  2. UE4物理模块(二)---建立物体碰撞

    在前文中介绍了什么是物理以及如何在UE4和PhysX中进行可视化调试: Jerry:UE4物理模块(一)---概述与可视化调试​zhuanlan.zhihu.com 这里调试只谈到了碰撞盒(后续还会有 ...

  3. R语言可视化--颜色

    RColorBrewer包 三类调色板:sequential / diverging / qualitative 调色板的信息可以与colorRamp / colorRampPalette结合使用 从 ...

  4. VS插件集合

    VS的强大不仅仅在于VS本身的强大. 同时,也有很多好用的插件,可以帮助我们更好的编辑代码, 提高效率. https://marketplace.visualstudio.com/search?tar ...

  5. js面向对象开发基础

    js的面向对象开发能力较弱,基本是以prototype为核心的面向对象,虽然现在出了个class这玩意,但本文还是先不做探讨. 面向对象基础——构造函数方法 var Fly = function (s ...

  6. LintCode 合并二维数组

    合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 之前想的是用剑指offer里替换空格那种方法 但是把 ...

  7. 跟阿根一起学Java Web开发二:使用Ajax技术及XML与JSON实现输出

    如今B/S结构的系统使用Ajax技术是再平常只是的了.今天我们就来探讨下在JSPGenSDF第四版中:怎样使用Ajax技术.怎样输出XML文件及JSON格式数据输出. 怎样搭建一个最基础的JSPGen ...

  8. 时间模块(import time)

    时间戳时间: float数据类型,给机器用的 print(time.time()) =>1533713657.5423343 结构化时间: 上下两种格式的中间状态 能够通过属性名来获取对象中的值 ...

  9. MSSQL 为db创建user

    use [IBatisNet]GO if not exists (select * from master.dbo.syslogins where loginname = N'IBatisNet')B ...

  10. MUI - 打开页面默认弹出键盘及返回关闭键盘

    打开页面默认弹出键盘及返回关闭键盘 http://www.cnblogs.com/phillyx/ (function(keyboard) { var openSoftKeyboard = funct ...