【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为真.与 ...
随机推荐
- 【JavaScript】ReactJS&NodeJS了解资料
ReactJS: GitHub:https://github.com/facebook/react React 入门实例教程:http://www.ruanyifeng.com/blog/2015/0 ...
- ecshop设置一个子类对应多个父类并指定跳转url的修改方法
这是一篇记录在日记里面的技术文档,其实是对ecshop的二次开发.主要作用是将一个子类对应多个父类,并指定条跳转url的功能.ecshop是一款在线购物网站,感兴趣的可以下载源码看看.我们看看具体是怎 ...
- codeforces B. The Fibonacci Segment 解题报告
题目链接:http://codeforces.com/problemset/problem/365/B 题目意思:简单来说,就是要找出最长的斐波纳契长度. 解决的方法不难,但是要注意更新左区间和右区间 ...
- 双系统重装windows后修复UBUNTU的GRUB
1.问题背景 本子是win7和ubuntu10.04双系统,用的还算好,虽然只有在用QQ的时候还会用到win7,但还是保留windows.可是几天 前,win7突然总是蓝屏.死机,更重要是的背景变成黑 ...
- ubuntu安装vmware tools
1.选择虚拟机菜单栏--安装VMware tools 选择你装虚拟机下的目录,点击 Linux.iso 再重新点击安装VMware Tools 这样会弹出一个包VMwareTools-9.6.0-1 ...
- SPI试验---verilog(实用单通模式)
SPI通信的读写操作 一. SPI简介: SPI的通信原理很简单,它以主从方式工作,这种模式通常有一个主设备和一个或多个从设备,需要至少4根线,事实上3根也可以(单向传输时).也是所有基于SP ...
- php 获取当前时间
<?php echo $showtime=date("Y-m-d H:i:s");?>
- Solr常用查询语法笔记
1.常用查询 q - 查询字符串,这个是必须的.如果查询所有*:* ,根据指定字段查询(Name:张三 AND Address:北京) fq - (filter query)过虑查询,作用:在q查询符 ...
- Maven打包部署脚本
#!/bin/sh SVN_USER_NAME= SVN_PASSWORD= ROOT_PATH PROJECT_NAME ACTION SVN_LIB_ROOT USER_ACCOUNT PASSW ...
- Java Hour 37 Weather ( 10 )
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 36 Weather 从失败的地方爬起来 在jsp 中,使用EL 表 ...