【POJ】1279 Art Gallery
http://poj.org/problem?id=1279
题意:给一个n个点的多边形,n<=1500,求在多边形内能看到所有多边形上的点的面积。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const double eps=1e-6;
const int N=1515;
int dcmp(double x) { return abs(x)<eps?0:(x<0?-1:1); }
struct iP { double x, y; iP(double _x=0, double _y=0) : x(_x), y(_y) {} };
typedef iP iV;
iV operator - (iP a, iP b) { return iV(a.x-b.x, a.y-b.y); }
iP operator + (iP a, iV b) { return iP(a.x+b.x, a.y+b.y); }
iV operator * (iP a, double d) { return iV(a.x*d, a.y*d); }
double cross(iV a, iV b) { return a.x*b.y-a.y*b.x; }
double angle(iV &a) { return atan2(a.y, a.x); } struct iL {
iV v; iP p;
double ang;
void set(iP a, iP b) { p=a; v=b-a; ang=angle(v); }
bool operator<(const iL &b) const { return ang<b.ang; }
};
iP LLi(iL &a, iL &b) {
static iV u;
static double t;
u=a.p-b.p;
t=cross(b.v, u)/cross(a.v, b.v);
return a.p+a.v*t;
}
bool onL(iP &a, iL &l) { return dcmp(cross(l.v, a-l.p))>0; }
bool half(iL *line, int n, iP *s, int &cnt) {
static iL a[N], q[N];
static iP b[N];
static int front, tail;
memcpy(a, line, sizeof(iL)*(n+1));
sort(a+1, a+1+n);
q[front=tail=0]=a[1];
for1(i, 2, n) {
while(front!=tail && !onL(b[tail-1], a[i])) --tail;
while(front!=tail && !onL(b[front], a[i])) ++front;
q[++tail]=a[i];
if(dcmp(cross(q[tail-1].v, q[tail].v))==0) {
--tail;
if(onL(a[i].p, q[tail])) q[tail]=a[i];
}
if(front!=tail) b[tail-1]=LLi(q[tail], q[tail-1]);
}
while(front!=tail && !onL(b[tail-1], q[front])) --tail;
if(tail-front<=1) return 0;
cnt=0;
b[tail]=LLi(q[tail], q[front]);
for1(i, front, tail) s[++cnt]=b[i];
return 1;
} iL line[N];
iP a[N], b[N];
int ln, n, num, flag;
void add(iP a, iP b) { ++ln; line[ln].set(a, b); } void clr() { num=ln=0; }
void readin() { read(n); for1(i, 1, n) scanf("%lf%lf", &a[i].x, &a[i].y); }
void build() {
a[n+1]=a[1];
for1(i, 1, n) add(a[i+1], a[i]);
}
void work() { flag=half(line, ln, b, num); }
void getans() {
if(!flag) { puts("0.00"); return; }
double ans=0;
b[num+1]=b[1];
for1(i, 1, num) ans+=b[i].x*b[i+1].y-b[i].y*b[i+1].x;
printf("%.2f\n", ans/2);
} int main() {
int ta=getint();
while(ta--) {
clr();
readin();
build();
work();
getans();
}
return 0;
}
裸的半平面交.................
【POJ】1279 Art Gallery的更多相关文章
- poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1279 Art Gallery - 求多边形核的面积
/* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...
- poj 1279 Art Gallery (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【POJ】【2068】Art Gallery
计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...
- 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉
DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $ 当然这里的$i$和$k$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
随机推荐
- mysql如何用order by 自定义排序
mysql如何用order by 自定义排序 id name roleId aaa bbb ccc ddd eee ,MySQL可以通过field()函数自定义排序,格式:field(value,st ...
- Linux 之 最常用的20条命令
玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...
- 【转】Mybatis/Ibatis,数据库操作的返回值
该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...
- 31.从尾到头输出链表[Print linked list from last to first]
[题目] 输入一个链表的头结点,从尾到头反过来输出每个结点的值. [分析] 这是一道很有意思的面试题.该题以及它的变体经常出现在各大公司的面试.笔试题中. [链表逆置] 看到这道题后,第一反应是从头到 ...
- Android 中的异步下载
网上提到最多的就是利用AsyncTask进行异步下载,用android-async-http第三方库的也比较多.这里写点注意事项. 先说说android-async-http,这个库发送请求利用thr ...
- 用RPM包安装MySQL的默认安装路径问题
在安装PHP时候要对一些配置选项进行设置,其中就有:--with-mysql[=DIR]:包含MySQL扩展,[=DIR]指定mysql安装目录,省略[=DIR]则为默认位置/usr--with-my ...
- TokuDB的特点验证
随着数据量越来越大,越来越频繁的遇到需要进行结构拆分的情况,每一次拆分都耗时很久,并且需要多方配合,非常的不想搞这个事情.于是在@zolker的提醒下想到了13年开源tokuDB,来解决我们迫在眉睫的 ...
- 修改iptables防火墙规则解决vsftp登录后不显示文件目录的问题
如果设置防火墙开端口可能只是常用的几个端口,这样很可能导vsftpd在被动模式时无法启动随机端口,从而造成客户端的FTP无法列出目录这样胡问题.解决方式很简单,给 vsftpd增加随机端口范围,然后把 ...
- cut mysqladmin
[root@86 ~]# mysqladmin -uroot -p123456 -S /tmp/mysql.sock status Uptime: 112403 Threads: 17 Questio ...
- jQuery信息提示工具jquery.poshytip (转载)
转载地址:http://www.helloweba.com/view-blog-123.html Poshy Tip是一款非常友好的信息提示工具,它基于jQuery,当鼠标滑向链接时,会出现一个信息提 ...