hdu3511-Prison Break
纪念一下人生中第一道扫描线算法的题。。。。。其实不是严格上的第一道。。。第一次遇到的那个至今没过。。。。。
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3511
这题应该算是扫描线的基础题。
对于每个圆,进入和出去的时候分别做扫描线,分别是x = c[i].x - c[i].r, x' = c[i].x + c[i].r;
这样对于每条扫描线,对于x',我们在集合中删去这个圆,这很好理解。
对于x,我们将这个圆加入集合,这样我们只要找向上第一个交的点和向下第一个交的点即可。
不过问题来了,这个交点坐标随着扫描线的移动是在变化的,但我们注意到,这些点的相对位置并不变(即上面的还在上面,下面的还在下面)
这样我们可以拉个set来维护扫描线上交点纵坐标,这个没必要存这个纵坐标的具体值,因为相对位置不变,树的结构也不变。
只需要重载一下小于号在insert时候使用即可。
如果扫描线向上或向下没有点,那么这个圆的depth = 1;
如果上下两个点属于同一个圆id,那么这个圆的depth = depth[id]+1;
如果上下两个圆属于不同的圆id1,id2,那么这个圆的depth = max(depth[id1], depth[id2]);
AC代码:
#include <bits/stdc++.h> using namespace std;
const int maxn = + ; typedef struct Circle{
int id, x, y, r; Circle( int id = , int x = , int y = , int r = ){
this->id = id;
this->x = x;
this->y = y;
this->r = r;
}
}Circle; Circle c[maxn]; typedef struct Point{
int id, ty; Point( int id = , int ty = ){
this->id = id;
this->ty = ty;
}
}Point; set<Point> s; typedef struct Line{
int id, x, ty; Line( int id = , int x = , int ty = ){
this->id = id;
this->x = x;
this->ty = ty;
} bool operator < ( const Line& l )const{
if( x == l.x )
return id < l.id;
return x < l.x;
}
}Line; Line l[maxn<<]; int depth[maxn]; int n, ans, xlog; double intersector( const Point& p ){
int id = p.id;
double r = 1.0*c[id].r; double x = 1.0*(xlog - c[id].x);
double y = sqrt((r*r - x*x)); if(p.ty == )
return 1.0*c[id].y + y;
else
return 1.0*c[id].y - y;
} bool operator < ( const Point& p1, const Point& p2 ){
if(p1.id == p2.id)
return p1.ty < p2.ty;
return intersector(p1) < intersector(p2);
} void solve( int nn ){
memset(depth, , sizeof(depth));
s.clear();
ans = ; for( int i = ; i < nn; ++i ){
int id = l[i].id, ty = l[i].ty;
xlog = l[i].x;
//cout << "id: " << id << " ty: " << ty << endl; if( ty == ){
s.erase(Point(id, ));
s.erase(Point(id, ));
}else{
s.insert(Point(id, ));
set<Point>::iterator it1 = s.find(Point(id, )), it2;
it2 = it1;
it2++; if( it1 == s.begin() || it2 == s.end() ){
depth[id] = ;
//cout << "id: " << id << " depth[id]: " << depth[id] << endl;
}else{
it1--;
if( it1->id == it2->id ){
depth[id] = depth[it1->id]+;
}else{
depth[id] = max( depth[it1->id], depth[it2->id] );
}
//cout << "id: " << id << " depth[id]: " << depth[id] << endl;
}
s.insert(Point(id, ));
}
ans = max( ans, depth[id]);
} printf("%d\n", ans);
} int main(void){
while(scanf("%d", &n) != EOF){
memset( c, , sizeof(c) );
memset( l, , sizeof(l) ); for( int i = ; i < n; ++i ){
c[i].id = i;
scanf("%d%d%d", &c[i].x, &c[i].y, &c[i].r);
l[i*] = Line(i, c[i].x-c[i].r, );
l[i*+] = Line(i, c[i].x+c[i].r, );
}
int nn = n * ;
sort( l, l + nn ); solve(nn);
} return ;
} /*
5
0 0 100
0 0 1
0 5 3
3 0 2
0 0 200
6
0 0 100
0 0 1
0 5 3
0 5 2
3 0 2
0 0 200
*/
hdu3511-Prison Break的更多相关文章
- hdu3511 Prison Break 圆的扫描线
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511 题目: Prison Break Time Limit: 10000/5000 MS ( ...
- HDU 3681 Prison Break(BFS+二分+状态压缩DP)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- hdu 3681 Prison Break (TSP问题)
Prison Break Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- Prison Break
Prison Break 时间限制: 1 Sec 内存限制: 128 MB提交: 105 解决: 16[提交][状态][讨论版] 题目描述 Scofild又要策划一次越狱行动,和上次一样,他已经掌 ...
- HDU3681 Prison Break
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 1254 - Prison Break
1254 - Prison Break PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Mic ...
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
- hdu 3681 Prison Break
http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个n*m的矩阵,'F'是起点.机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所 ...
- light oj 1254 - Prison Break 最短路
题目大意:n个点m条边的有向图,q次询问c,s,t,表示汽车邮箱容量为c,求从起点s到终点t的最小费用.汽车在每个点可以加任意的油,每个点的单位油价为a[i]. 题目思路:利用最小费优先队列优化最短路 ...
随机推荐
- java中“53”个关键字(含2个保留字)
1.java的关键字(keyword)有多少个? 51+2个保留字=53个关键字(java的关键字都是小写的!!) 2.java的保留字(reserve word)有多少个?问题:分别是什么? 2个保 ...
- img图片加载出错处理
img图片加载出错处理 为了美观当网页图片不存在时不显示叉叉图片 当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验.即使使用alt属性给出了” ...
- linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号
linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号 实现的功能是吧换行符换成逗号了,自己试验过. 求解释,:t N b t 都是什么意思??? :t 定义la ...
- enote笔记语言(4)(ver0.4)——“5w1h2k”分析法
章节:“5w1h2k”分析法 what:我想知道某个“关键词(keyword)”(即,词汇.词语,或称单词,可以是概念|专业术语|.......)的定义. why:我想分析and搞清楚弄明白“事物 ...
- Git 基础教程 之 标签
所谓标签:就是一个让人容易记住的有意义的名字,与某个commit绑在一起. 创建标签:①切回需要打标签的分支上 ② git tag <name> 默认标 ...
- java 同时安装多版本问题
java 同时安装多版本问题(转) http://www.cnblogs.com/SamuelSun/p/6022296.html http://blog.csdn.net/u013256622/ar ...
- 洛谷P3375【模板】KMP字符串匹配
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如果你不知道这是什么意思也不要问,去百度 ...
- App架构设计经验谈:接口”安全机制”的设计
[原文地址 点击打开链接] 原创文章,转载请注明:转载自Keegan小钢 并标明原文链接:http://keeganlee.me/post/architecture/20160107 微信订阅号:ke ...
- printf 打印字符串的任意一部分
使用printf()函数打印字符串的任意部分,请看下例: <span style="font-size:16px;">#include <stdio.h> ...
- 无管理员帐号的WIN7,如果使用自己的JDK版本?
因为公司的电脑只有普通权限, 而且JDK版本低了. 那我只好用BAT脚本来导入自己的环境啦,毕竟每次在CMD窗口输入太繁琐. set JAVA_HOME=D:\JDK8 set CLASSPATH=D ...