【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 ...
随机推荐
- 第二回 C#和JAVA 语法差异性对比
1.继承 C#用 : java用 extends 继承父类 implements 2.Java : 一个源文件中只能有一个public类 可以有多个非public类 源文件的名称应该和pu ...
- Javascript的V8引擎研究
1.针对上下文的Snapshot技术 什么是上下文(Contexts)?实际是JS应用程序的运行环境,避免应用程序的修改相互影响,例如一个页面js修改内置对象方法toString,不应该影响到另外页面 ...
- 【算法专题】后缀自动机SAM
后缀自动机是用于识别子串的自动机. 学习推荐:陈立杰讲稿,本文记录重点部分和感性理解(论文语言比较严格). 刷题推荐:[后缀自动机初探],题目都来自BZOJ. [Right集合] 后缀自动机真正优于后 ...
- 网络爬虫框架Heritrix中Modules的各项说明
1)Select Crawl Scope:Crawl Scope 用于配置当前应该在什么范围内抓取网页链接.例如选择 BroadScope 则表示当前的抓取范围不受限制,选择 HostScope 则表 ...
- 浅谈iOS与社交化网络
CHENYILONG Blog 社交化网络 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong ...
- 判断html是否含有图片
核心代码: $url="http://XXXXX/article/012.html"; $content=file_get_contents($url); //读取文章页面源代码 ...
- jquery $.post() 向php传值 实现简单的二级联动
更多内容推荐微信公众号,欢迎关注: 1 其中selectid是一个下拉菜单的id $().ready(function () { $("#selectid").change(fun ...
- [转]双线性插值(Bilinear interpolation)
1,原理 在图像的仿射变换中,很多地方需要用到插值运算,常见的插值运算包括最邻近插值,双线性插值,双三次插值,兰索思插值等方法,OpenCV提供了很多方法,其中,双线性插值由于折中的插值效果和运算速度 ...
- Spring4笔记11--SSH整合2--SpringWeb
SSH 框架整合技术: 2. Spring 在 Web 项目中的使用(建立在Spring与Hibernate整合的基础上): 在 Web 项目中使用 Spring 框架,首先要解决在 Servlet ...
- 连续的if语句
use_relu=0 use_tanh=2 a = 2 if use_relu else (1 if use_tanh else 0)#如果use_relu不等于0,则a等于2:如果use_relu等 ...