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的更多相关文章

  1. poj 1279 -- Art Gallery (半平面交)

    鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  2. poj 1279 Art Gallery - 求多边形核的面积

    /* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...

  3. poj 1279 Art Gallery (Half Plane Intersection)

    1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...

  4. 【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, ...

  5. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  6. 【POJ】【2068】Art Gallery

    计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...

  7. 【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$都是偶数啦~ ...

  8. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  9. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

随机推荐

  1. Maven使用常见问题整理

    Maven使用常见问题整理  1.更新eclipse的classpath加入新依赖  1.在dependencyManagement里面加入包括版本在内的依赖信息,如:   <dependenc ...

  2. BestCoder Round #60 1001

    Problem Description You are given a sequence of NNN integers. You should choose some numbers(at leas ...

  3. Sublime Text 2 入门及技巧

    看了 Nettuts+ 对 Sublime Text 2 的介绍, 立刻就兴奋了,诚如作者 Jeffrey Way 所说:“<永远的毁灭公爵>都发布了,TextMate 2 还没发”,你还 ...

  4. MYSQL索引失效的各种情形总结

    1) 没有查询条件,或者查询条件没有建立索引  2) 在查询条件上没有使用引导列  3) 查询的数量是大表的大部分,应该是30%以上.  4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索 ...

  5. HTML表单元素登陆界面

    <form action="" method="post" name="myform"><p>E-mail:< ...

  6. 为eclipse设置好看的代码主题

    eclipse的默认代码背景是白色,上个文章简单说了字体设置,这边主要介绍代码高亮的主题设置,打造更酷的编程界面.网上有文章说可以在设置里面逐一设置,但是比较麻烦,可以去网上下载现成的主题包,网址为: ...

  7. Java for LeetCode 070 Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. July 17th, Week 30th Sunday, 2016

    You are beautiful, but that is not why I love you. 你如此美丽,但我并非因此而爱你. Although we have always been tol ...

  9. 关于struts 2中的日期问题

    struts 2中引入了大量的jquery的内容 其中日期问题总结一下: 步骤: 1.当然不用说,先建一个web项目 2.导入struts2所需要的jar包,以及此插件的包(当然你也可以用:strut ...

  10. linux下notify机制(仅用于内核模块之间的通信)

    1.通知链表简介 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子系统,Linux内核提供了通知链的机制 ...