【POJ 1279】Art Gallery
http://poj.org/problem?id=1279
裸的半平面交的模板,按极角排序后维护一个双端队列,不要忘了最后要去除冗余,即最后一条边(或者更多的边)一定在双端队列里,但它不一定构成半平面,所以要特判。
还有平行的边也要特判,因为平行的边的交点不可求!
最后在poj上用G++交WA了好几次,换用C++就AC了/(ㄒoㄒ)/~~
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const double Pi = acos(-1.0);
const int N = 1503;
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 3) + (k << 1) + c - '0';
return k * fh;
} struct Point {
double x, y;
Point(double _x = 0, double _y = 0) : x(_x), y(_y) {}
} t[N], p[N];
struct Line {
Point p, v;
double ang;
Line(Point _p = Point(0, 0), Point _v = Point(0, 0), double _ang = 0) : p(_p), v(_v), ang(_ang) {}
bool operator < (const Line &x) const {
return ang < x.ang;
}
} l[N], q[N]; Point operator + (Point a, Point b) {return Point(a.x + b.x, a.y + b.y);}
Point operator - (Point a, Point b) {return Point(a.x - b.x, a.y - b.y);}
Point operator * (Point a, double x) {return Point(a.x * x, a.y * x);}
Point operator / (Point a, double x) {return Point(a.x / x, a.y / x);} double dcmp(double x) {return fabs(x) < 1e-8 ? 0 : (x < 0 ? -1 : 1);}
double Dot(Point a, Point b) {return a.x * b.x + a.y * b.y;}
double Cross(Point a, Point b) {return a.x * b.y - a.y * b.x;}
double sqr(double x) {return x * x;}
double dis(Point a, Point b) {return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}
bool onleft(Point a, Line b) {return dcmp(Cross(a - b.p, b.v)) < 0;}
Point intersection(Line a, Line b) {
Point u; double t;
u = a.p - b.p;
t = Cross(b.v, u) / Cross(a.v, b.v);
return a.p + a.v * t;
} int n; double mkhalf() {
q[1] = l[1]; q[2] = l[2];
p[1] = intersection(q[1], q[2]); int head = 1, tail = 2;
for(int i = 3; i <= n; ++i) {
while (head < tail && !onleft(p[tail - 1], l[i])) --tail;
while (head < tail && !onleft(p[head], l[i])) ++head;
q[++tail] = l[i];
if (dcmp(Cross(q[tail].v, q[tail - 1].v)) == 0) {
--tail;
if (onleft(l[i].p, q[tail])) q[tail] = l[i];
}
if (head < tail) p[tail - 1] = intersection(q[tail - 1], q[tail]);
}
while (head < tail && !onleft(p[tail - 1], q[head])) --tail;
p[tail] = intersection(q[head], q[tail]); if (tail - head <= 1) return 0;
else {
double ret = 0;
p[tail + 1] = p[head];
for(int i = head; i <= tail; ++i)
ret += Cross(p[i], p[i + 1]);
return ret / 2;
}
} int main() {
int T = in();
while (T--) {
n = in();
for(int i = 1; i <= n; ++i) scanf("%lf%lf", &t[i].x, &t[i].y);
t[n + 1] = t[1];
for(int i = 1; i <= n; ++i)
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)); sort(l + 1, l + n + 1); printf("%.2lf\n", mkhalf());
} return 0;
}
_(:з」∠)_
【POJ 1279】Art Gallery的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
		2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ... 
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
		2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ... 
- BZOJ2288: 【POJ Challenge】生日礼物
		2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ... 
- BZOJ2293: 【POJ Challenge】吉他英雄
		2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ... 
- BZOJ2287: 【POJ Challenge】消失之物
		2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ... 
- BZOJ2295: 【POJ Challenge】我爱你啊
		2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ... 
- BZOJ2296: 【POJ Challenge】随机种子
		2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ... 
- BZOJ2292: 【POJ Challenge 】永远挑战
		2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ... 
- 【POJ 1125】Stockbroker Grapevine
		id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ... 
随机推荐
- AC日记——单词翻转 1.7 27
			27:单词翻转 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个句子(一行),将句子中的每一个单词翻转后输出. 输入 只有一行,为一个字符串,不超过500个字符.单词之间以空 ... 
- 平滑过渡的战争迷雾(一) 原理:Warcraft3地形拼接算法
			本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9611887 作者:七十一雾央 新浪微博:http:/ ... 
- Nginx负载均衡配置
			1.yum安装nginx yum install nginx 2.启动nginx chkconfig nginx on service nginx start向web服务器中放入测试文件: < ... 
- 千位分隔符(js 实现)
			最近被同事问到js如何实现给长数字添加千位分隔符,即 1344444 ---> 13,444,444 这是一个很常见的前端面试题.看起来简单,刚开始我都懒得写. 仔细一想,挺考逻辑的,实现方法有 ... 
- spring mvc总结1
			1,spring下载 spring更改了官方网站后,找了很长时间没有找到相关的jar包下载路径,然后在网上终于找到相关的路径了 有个树形结构可供选择:http://repo.spring.io/rel ... 
- Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例
			概要 这一章,我们对HashSet进行学习.我们先对HashSet有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashSet.内容包括:第1部分 HashSet介绍第2部分 HashSe ... 
- HTML5商城开发一 楼层滚动加载数据
			对于楼层加载在以前只是个想法,从来没实现过,刚好项目中碰到,再此总结一下 场景:HTML5,局部商品列表信息滚动(局部滚动条) 1.通过jq设置subCategoryScroll的高度为屏幕显示高度( ... 
- 投入Html5的怀抱,最近在研究的Egret
			html5没有办法不关注,实在太火热了,几年前还不行,如今确是环境较好,typescript语言很好学习,可能基于之前的基础,不到一个星期就基本上差不多了,虽然还有一些小问题,但那都是经验积累下来可以 ... 
- Windows 8.1 新增控件之 Flyout
			本篇为大家介绍Flyout 控件,Flyout 属于一种轻量级交互控件,可以支持信息提示或用户交互.与传统Dialog 控件不同的是Flyout 控件可通过直接点击对话框外部区域忽略. Flyout ... 
- Linux socket多进程服务器框架一
			重点:socket共用方法中错误码的定义以及错误码的解析 底层辅助代码 //serhelp.h #ifndef _vxser #define _vxser #ifdef __cplusplus ext ... 
