POJ2451 Uyuw's Concert
来源:互联网 发布:团队行程软件 编辑:程序博客网 时间:2024/04/29 15:16
听大佬说这题要用O(nlogn)的半平面交过,于是我。。。。我就继续使用我的O(nlogn)的板子,哈哈哈哈哈嗝
板子用的kuangbin大佬的http://www.cnblogs.com/kuangbin/p/3266097.html
题意:在(0,10000)*(0,10000)的坐标系上,给定n个半平面,求出它们围成的图形的面积
把坐标边界线加进去,叉积求面积就OK 啦~~
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include<cmath>using namespace std;const double eps = 1e-8;const double PI = acos(-1.0);int sgn(double x){ if(fabs(x) < eps) return 0; if(x < 0) return -1; else return 1;}struct Point{ double x,y; Point(){} Point(double _x,double _y) { x = _x; y = _y; } Point operator -(const Point &b)const { return Point(x - b.x, y - b.y); } double operator ^(const Point &b)const { return x*b.y - y*b.x; } double operator *(const Point &b)const { return x*b.x + y*b.y; }};struct Line{ Point s,e; double k; Line(){} Line(Point _s,Point _e) { s = _s; e = _e; k = atan2((e.y - s.y),(e.x - s.x)); } Point operator &(const Line &b)const { Point res = s; double t = ((s - b.s)^(b.s - b.e))/((s - e)^(b.s - b.e)); res.x += (e.x - s.x)*t; res.y += (e.y - s.y)*t; return res; }};//半平面交,直线的左边代表有效区域bool HPIcmp(Line a,Line b){ if(fabs(a.k - b.k) > eps)return a.k < b.k; return ((a.s - b.s)^(b.e - b.s)) < 0;}Line Q[20005];void HPI(Line line[], int n, Point res[], int &resn){ int tot = n; sort(line,line+n,HPIcmp); tot = 1; for(int i = 1;i < n;i++) if(fabs(line[i].k - line[i-1].k) > eps) line[tot++] = line[i]; int head = 0, tail = 1; Q[0] = line[0]; Q[1] = line[1]; resn = 0; for(int i = 2; i < tot; i++) { if(fabs((Q[tail].e-Q[tail].s)^(Q[tail-1].e-Q[tail-1].s)) < eps || fabs((Q[head].e-Q[head].s)^(Q[head+1].e-Q[head+1].s)) < eps) return; while(head < tail && (((Q[tail]&Q[tail-1]) - line[i].s)^(line[i].e-line[i].s)) > eps) tail--; while(head < tail && (((Q[head]&Q[head+1]) - line[i].s)^(line[i].e-line[i].s)) > eps) head++; Q[++tail] = line[i]; } while(head < tail && (((Q[tail]&Q[tail-1]) - Q[head].s)^(Q[head].e-Q[head].s)) > eps) tail--; while(head < tail && (((Q[head]&Q[head-1]) - Q[tail].s)^(Q[tail].e-Q[tail].e)) > eps) head++; if(tail <= head + 1)return; for(int i = head; i < tail; i++) res[resn++] = Q[i]&Q[i+1]; if(head < tail - 1) res[resn++] = Q[head]&Q[tail];}Point p[20005];//p数组存结果Line line[20005];double xmult(Point p0,Point p1,Point p2){/*p0p1 X p0p2*/ return (p1-p0)^(p2-p0);}int main(){ int n; while(~scanf("%d",&n)) { for(int i = 0;i <n;i++){ scanf("%lf%lf",&p[i].x,&p[i].y); scanf("%lf%lf",&p[i+1].x,&p[i+1].y); line[i] = Line(p[i],p[i+1]); } line[n++]=Line( Point(0, 0), Point(10000, 0)); line[n++]=Line( Point(10000, 0), Point(10000, 10000)); line[n++]=Line( Point(10000, 10000), Point(0, 10000)); line[n++]=Line( Point(0, 10000), Point(0, 0)); int resn; double area=0.0; HPI(line,n,p,resn); for(int i=1;i<resn-1;i++) area+=xmult(p[0],p[i],p[i+1]); if(resn<3) area=0; if(area<0) area=-area; printf("%.1f\n",area/2.0); } return 0;}
阅读全文
0 0
- POJ2451-Uyuw's Concert
- POJ2451 Uyuw's Concert
- 【半平面交】[POJ2451]Uyuw's Concert
- 【半平面交】[POJ2451]Uyuw's Concert
- poj2451 Uyuw's Concert【半平面交】
- [poj2451]Uyuw's Concert 半平面交
- poj2451——Uyuw's Concert//半平面交
- [POJ2451]Uyuw's Concert(半平面交)
- POJ1279/ZOJ1369 Art Gallery,POJ2451 Uyuw's Concert(半平面交求多边形的核)
- poj2451-Uyuw's Concert (求半平面围成的面积)
- Uyuw's Concert POJ
- POJ 2451 Uyuw's Concert
- POJ 2451 Uyuw's Concert
- POJ 2451 Uyuw's Concert
- 【Poj 2451】Uyuw's Concert
- POJ 2451 Uyuw's Concert
- POJ 2451 Uyuw's Concert 题解
- POJ 2451 Uyuw's Concert(半平面交nlgn)
- tomcat 7.0安装过程详解
- (译)使用Kotlin和RxJava处理复杂的请求
- 自定义层次阴影
- kafka学习一:原理和概念
- Unity 动画系统-AnimationEvent
- POJ2451 Uyuw's Concert
- Android NDK 运行错误:java.lang.UnsatisfiedLinkError: Couldn't load XXX indLibrary returned null
- 基于GraphCuts图割算法的图像分割----OpenCV代码与实现
- c语言判断机器大小端
- HDU 1081 DP找最大和的矩阵
- 第一篇博客
- SPOJ VLATTICE Visible Lattice Points[莫比乌斯反演]
- mysql存储过程的学习(mysql提高执行效率之进阶过程)
- pycharm 每日提示