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

裸的半平面交的模板,按极角排序后维护一个双端队列,不要忘了最后要去除冗余,即最后一条边(或者更多的边)一定在双端队列里,但它不一定构成半平面,所以要特判。

还有平行的边也要特判,因为平行的边的交点不可求!

最后在poj上用G++交WA了好几次,换用C++就AC了/(ㄒoㄒ)/~~

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const double Pi = acos(-1.0);
  7. const int N = 1503;
  8. int in() {
  9. int k = 0, fh = 1; char c = getchar();
  10. for(; c < '0' || c > '9'; c = getchar())
  11. if (c == '-') fh = -1;
  12. for(; c >= '0' && c <= '9'; c = getchar())
  13. k = (k << 3) + (k << 1) + c - '0';
  14. return k * fh;
  15. }
  16.  
  17. struct Point {
  18. double x, y;
  19. Point(double _x = 0, double _y = 0) : x(_x), y(_y) {}
  20. } t[N], p[N];
  21. struct Line {
  22. Point p, v;
  23. double ang;
  24. Line(Point _p = Point(0, 0), Point _v = Point(0, 0), double _ang = 0) : p(_p), v(_v), ang(_ang) {}
  25. bool operator < (const Line &x) const {
  26. return ang < x.ang;
  27. }
  28. } l[N], q[N];
  29.  
  30. Point operator + (Point a, Point b) {return Point(a.x + b.x, a.y + b.y);}
  31. Point operator - (Point a, Point b) {return Point(a.x - b.x, a.y - b.y);}
  32. Point operator * (Point a, double x) {return Point(a.x * x, a.y * x);}
  33. Point operator / (Point a, double x) {return Point(a.x / x, a.y / x);}
  34.  
  35. double dcmp(double x) {return fabs(x) < 1e-8 ? 0 : (x < 0 ? -1 : 1);}
  36. double Dot(Point a, Point b) {return a.x * b.x + a.y * b.y;}
  37. double Cross(Point a, Point b) {return a.x * b.y - a.y * b.x;}
  38. double sqr(double x) {return x * x;}
  39. double dis(Point a, Point b) {return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}
  40. bool onleft(Point a, Line b) {return dcmp(Cross(a - b.p, b.v)) < 0;}
  41. Point intersection(Line a, Line b) {
  42. Point u; double t;
  43. u = a.p - b.p;
  44. t = Cross(b.v, u) / Cross(a.v, b.v);
  45. return a.p + a.v * t;
  46. }
  47.  
  48. int n;
  49.  
  50. double mkhalf() {
  51. q[1] = l[1]; q[2] = l[2];
  52. p[1] = intersection(q[1], q[2]);
  53.  
  54. int head = 1, tail = 2;
  55. for(int i = 3; i <= n; ++i) {
  56. while (head < tail && !onleft(p[tail - 1], l[i])) --tail;
  57. while (head < tail && !onleft(p[head], l[i])) ++head;
  58. q[++tail] = l[i];
  59. if (dcmp(Cross(q[tail].v, q[tail - 1].v)) == 0) {
  60. --tail;
  61. if (onleft(l[i].p, q[tail])) q[tail] = l[i];
  62. }
  63. if (head < tail) p[tail - 1] = intersection(q[tail - 1], q[tail]);
  64. }
  65. while (head < tail && !onleft(p[tail - 1], q[head])) --tail;
  66. p[tail] = intersection(q[head], q[tail]);
  67.  
  68. if (tail - head <= 1) return 0;
  69. else {
  70. double ret = 0;
  71. p[tail + 1] = p[head];
  72. for(int i = head; i <= tail; ++i)
  73. ret += Cross(p[i], p[i + 1]);
  74. return ret / 2;
  75. }
  76. }
  77.  
  78. int main() {
  79. int T = in();
  80. while (T--) {
  81. n = in();
  82. for(int i = 1; i <= n; ++i) scanf("%lf%lf", &t[i].x, &t[i].y);
  83. t[n + 1] = t[1];
  84. for(int i = 1; i <= n; ++i)
  85. l[i] = Line(t[i + 1], t[i] - t[i + 1], atan2(t[i].y - t[i + 1].y, t[i].x - t[i + 1].x));
  86.  
  87. sort(l + 1, l + n + 1);
  88.  
  89. printf("%.2lf\n", mkhalf());
  90. }
  91.  
  92. return 0;
  93. }

_(:з」∠)_

【POJ 1279】Art Gallery的更多相关文章

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  3. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  4. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  5. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  6. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  7. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  8. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

  9. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

随机推荐

  1. UVALive 5066 Fire Drill --BFS+DP

    题意:有一个三维的地图,有n个人被困住,现在消防队员只能从1楼的一个入口进入,营救被困者,每一个被困者有一个价值,当消防队员找到一个被困者之后,他可以营救或者见死不救,如果救的话,他必须马上将其背到入 ...

  2. UVA 12716 GCD XOR【异或】

    参考:http://www.cnblogs.com/naturepengchen/articles/3952145.html #include<stdio.h> #include<s ...

  3. Hash MD5 CRC 知识

    本文旨在科普安全相关的知识,并附一个C#实现的文件管理工具. Hash 安全散列算法(英语:Secure Hash Algorithm,缩写为SHA)是一个密码散列函数家族,是FIPS所认证的五种安全 ...

  4. JavaScript Number 对象

    JavaScript Number 对象 Number 对象 Number 对象是原始数值的包装对象. Number 创建方式 new Number(). 语法 var num = new Numbe ...

  5. Lobes of the brain

    Source: https://en.wikipedia.org/wiki/Lobes_of_the_brain (Except for the last figure) Terminologia A ...

  6. BZOJ 1082 【SCOI2005】 栅栏

    Description 农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材.于是农夫约翰到木材店购买木材.可是木材店老板说他这里只剩下少部分大规格的木板了.不过约翰可以购买这些木 ...

  7. gRpc NET Core

    NET Core下使用gRpc公开服务(SSL/TLS) 一.前言 前一阵子关于.NET的各大公众号都发表了关于gRpc的消息,而随之而来的就是一波关于.NET Core下如何使用的教程,但是在这众多 ...

  8. Ros与Vrep平台搭建

    参考资料: ROS安装: ros 是一个framework 和Android有点像,内核用的linux.Ros提供了一种供机器人开发者迅速上手的一个平台, 可以快速搭建自己的应用,利用ros下面自带的 ...

  9. 利用Spring MVC搭建REST Service

    之前写过一篇 利用JAX-RS快速开发RESTful 服务 今天来看下spring-mvc框架如何实现类似的功能: 一.pom.xml <?xml version="1.0" ...

  10. Qt Creator 常用快捷键

    多行注释模式                                                                                            Ct ...