【POJ】2043.Area of Polygons
原题戳这里
开始一小段时间的POJ计算几何练习计划(估计很快就会被恶心回去)
题解
用一条平行于y轴的扫描线,计算两条扫描线之间多少格子被覆盖了
精度可tm变态了,可能是因为题目要求的关系吧,需要上取整和下取整,可能有一点误差也给算进去了,精度掉的很大
看一下上一次的上边界是哪里,不要重复计算
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <cstring>
#include <ctime>
#include <map>
#include <algorithm>
#include <cmath>
#define MAXN 100005
#define eps 1e-8
//#define ivorysi
#define PDD pair<db,db>
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int64;
typedef double db;
int N;
bool dcmp(db a,db b) {
return fabs(a - b) < eps;
}
bool Gter(db a,db b) {
return a > b + eps;
}
struct Point {
db x,y;
Point() {}
Point(db _x,db _y) {
x = _x;y = _y;
}
friend Point operator + (const Point &a,const Point &b) {
return Point(a.x + b.x,a.y + b.y);
}
friend Point operator - (const Point &a,const Point &b) {
return Point(a.x - b.x,a.y - b.y);
}
friend Point operator / (const Point &a,const db &k) {
return Point(a.x / k,a.y / k);
}
friend Point operator * (const Point &a,const db &k) {
return Point(a.x * k,a.y * k);
}
friend db operator * (const Point &a,const Point &b) {
return a.x * b.y - a.y * b.x;
}
}P[205];
db dot(Point a,Point b) {
return a.x * b.x + a.y * b.y;
}
struct Seg {
Point a,b;
Seg() {}
Seg(Point _a,Point _b) {
a = _a;b = _b;
}
}Line[205];
PDD Y[205];
void Solve() {
int ans = 0;
int st = 2000,ed = -2000;
for(int i = 1 ; i <= N ; ++i) {
scanf("%lf%lf",&P[i].x,&P[i].y);
st = min(st,(int)P[i].x);
ed = max(ed,(int)P[i].x);
}
P[N + 1] = P[1];
for(int i = 1 ; i <= N ; ++i) {
if(P[i].x > P[i + 1].x) Line[i] = Seg(P[i + 1],P[i]);
else Line[i] = Seg(P[i],P[i + 1]);
}
for(int i = st ; i <= ed ; ++i) {
int cnt = 0;
for(int j = 1 ; j <= N ; ++j) {
if(Line[j].a.x <= i && Line[j].b.x >= i + 1) {
db a = Line[j].a.y + (i - Line[j].a.x) * (Line[j].b.y - Line[j].a.y) / (Line[j].b.x - Line[j].a.x);
db b = Line[j].a.y + (i - Line[j].a.x + 1) * (Line[j].b.y - Line[j].a.y) / (Line[j].b.x - Line[j].a.x);
Y[++cnt] = mp(a,b);
}
}
sort(Y + 1,Y + cnt + 1);
int last = -4001;
for(int j = 1 ; j <= cnt ; j += 2) {
int f = floor(min(Y[j].fi,Y[j].se));
int c = ceil(max(Y[j + 1].fi,Y[j + 1].se));
f = max(f,last);
ans += c - f;
last = c;
}
}
printf("%d\n",ans);
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
while(scanf("%d",&N) != EOF && N) {
Solve();
}
return 0;
}
【POJ】2043.Area of Polygons的更多相关文章
- 【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 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【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为真.与 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
- 【POJ】【1637】Sightseeing tour
网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...
- 【poj】1001
[题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...
- 【POJ】3070 Fibonacci
[算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...
随机推荐
- NLP分词
英文分词: #英文分词 import nltk sentence="hello,world" tokens=nltk.word_tokenize(sentence) print(t ...
- idea 永久注册
1.在百度输入http://idea.lanyus.com/ 2.点击这个网址http://idea.lanyus.com/jar/JetbrainsCrack-3.1-release-enc.ja ...
- [转载]Node入门 » 一本全面的Node.js教程
http://www.nodebeginner.org/index-zh-cn.html 作者: Manuel Kiessling 翻译: goddyzhao & GrayZhang & ...
- 用一张图片解释清楚jQuery中10个强大的遍历函数
为什么我们要进一步提炼一系列元素,难道是jQuery选择语法不够强大?让我们从示例开始.在下面提到的网页中,当一个star被点击时,我们需要给它以及左边的每个star添加class"on&q ...
- [php]mysql操作流程
这种是比较老的一种mysql连接方法 1.连接数据库 $this->con = mysql_connect($this->host, $this->user, $this->p ...
- 是否使用TDD(测试驱动开发)进行UI开发
问题 StackOverflow上有一则是否使用TDD(测试驱动开发)进行UI开发 的提问. _JacobE_问: 对于是否使用TDD进行开发UI这件事,我想了很久,但难以决定.我想听听你们的意见. ...
- 【AtCoder Regular Contest 080E】Young Maids [堆][线段树]
Young Maids Time Limit: 50 Sec Memory Limit: 512 MB Description 给定一个排列,每次选出相邻的两个放在队头,要求字典序最小. Input ...
- html向js传递id
html获取id方法: <div id="thediv1" style="display:block" onclick="ceshi(this. ...
- 在Django中Session的那点事!
1.session是什么 首先引入度娘的解释:Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 We ...
- Python练习-不知道弄个什么鬼
Alex大神,今天丢过来一个PDF,结果就成了这个样子! 1. 执行 Python 脚本的两种方式 交互方式: 命令行 文件方式: ...