原题戳这里

开始一小段时间的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的更多相关文章

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

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

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

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

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

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

  5. 【POJ】1222 EXTENDED LIGHTS OUT

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

  6. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  7. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  8. 【poj】1001

    [题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

随机推荐

  1. 应用jfinal发送微信模板消息的一个bug

    严格来讲,这不是一个bug,只是我们应用的方式不对.微信发送模板消息的方法是: HttpUtils.post(sendApiUrl + AccessTokenApi.getAccessTokenStr ...

  2. 51 nod 1058 N的阶乘的长度

    1058 N的阶乘的长度 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.   In ...

  3. 腾讯高级设计师谈微信的旧容与新妆,Android Design是大势所趋

    编者按:本篇投稿选自腾讯大讲堂(更多腾讯产品技术文章,可以关注“腾讯大讲堂”微信公众账号),由腾讯研发管理部高级设计师Vertu撰写,他以产品设计师的视角,对比解读了微信的旧容与新妆,也讲了Andro ...

  4. soj1166. Computer Transformat(dp + 大数相加)

    1166. Computer Transformat Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description A sequenc ...

  5. 内核:为了fan的健康,我的重新编译记录

    email: jiqingwu@gmail.com date: 2008-02-13 关键词:ubuntu cpu cpufreqd cpufrequtils 编译 内核 装上ubuntu7.10后, ...

  6. 20155306 2016-2017-2 《Java程序设计》第6周学习总结

    20155306 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1 InputStream与OutputStream 如果要将数据 ...

  7. Linux ftp命令的使用方法 -- 转

    http://jingyan.baidu.com/article/066074d68b6a7ac3c21cb038.html FTP(File Transfer Protocol, FTP)是TCP/ ...

  8. Linux查看日志三种命令

    第一种:查看实时变化的日志(比较吃内存) 最常用的: tail -f filename (默认最后10行,相当于增加参数 -n 10) Ctrl+c 是退出tail命令   其他情况: tail -n ...

  9. 守卫者的挑战(guard)

    problem Pro 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如果你能通过 ...

  10. mybatis的配置文件中<selectKey>标签问题

    1.mybatis的配置文件中,使用sequence生成主键 未执行add方法之前,主键未生成(null):刚执行add之后,主键即生成(212) 这里的重点是,一旦执行add方法,配置文件中的sel ...