Uva1398--Meteor(扫描线)
来源:互联网 发布:linux mysql配置文件 编辑:程序博客网 时间:2024/05/29 10:37
题目大意:今晚有流星划过,求在某瞬间最多有多少流星能被拍进镜头?
分析:咋看之下有点复杂。一点点分析后,可以发现这题是借助扫描线来解决,类似区间并,面积并。
每个流星可以看做一个开区间,这个问题可以转化为,求扫描线在哪个位置时与最多的开区间相交。
首先,我们需要将流星转化为开区间,如何转化呢?题目给的数据有坐标x,y,以及xy方向上的速度,那么我们很自然地想到分解成xy两个方向上来求区间,然后得到的两个区间的相交部分,就是该流星的开区间。
然后,就可以用扫描线啦~
需要注意的是,当某一位置既有前一个区间的右端点,又有后一个区间的左端点时,我们先操作右端点,再操作左端点。很好理解,因为在镜头边缘的点,我们并不计数。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 111111;struct Event { int x; int type; bool operator < (const Event & cmp) { return x < cmp.x || (x == cmp.x && type > cmp.type); }}e[2*maxn];void update(int a, int x, int w, int& L, int& R) { if(a == 0) { if(x <= 0|| x >= w) R = L-1; } else if(a > 0) { L = max(L, -x*2520/a); R = min(R, (w-x)*2520/a); } else { L = max(L, (w-x)*2520/a); R = min(R, -x*2520/a); }}int main() { int T; scanf("%d", &T); while(T--) { int n, w, h; int a, b, x, y; scanf("%d%d%d", &w, &h, &n); int cnt = 0; for(int i = 0; i < n; i++) { scanf("%d%d%d%d", &x, &y, &a, &b); int L = 0, R = 1 << 30; update(a, x, w, L, R); update(b, y, h, L, R); if(R > L) { e[cnt++] = (Event){L, 0}; e[cnt++] = (Event){R, 1}; } } sort(e, e+cnt); int ans = 0; int tt = 0; for(int i = 0; i < cnt; i++) { if(e[i].type == 0) { tt++; ans = max(ans, tt); } else tt--; } printf("%d\n", ans); } return 0;}
0 0
- Uva1398--Meteor(扫描线)
- 【转换模型+扫描线】【UVA1398】Meteor
- uva1398 - Meteor
- Uva1398 Meteor
- uva1398 - Meteor
- uva1398 Meteor
- uva1398 线性扫描
- LA3905-Meteor(扫描线)
- UVA 1398 Meteor(扫描线)
- LA 3905 Meteor 扫描线 -
- LA 3905 Meteor [扫描线]
- uva1398
- UVALive 3905-Meteor-扫描线算法
- UVALive LA3905 Meteor (扫描线统计)
- UVa 1398 Meteor 解题报告(扫描线)
- LA 3905 Meteor / 区间扫描
- LA 3905 Meteor (排序+扫描法)
- meteor
- HDU 2256 Problem of Precision(矩阵快速幂 数论 )
- mysql索引失效的情形
- C++编程基础----2.1变量和基本类型
- 矩阵快速幂基本思想
- jvm内存模型
- Uva1398--Meteor(扫描线)
- 确定字符互异
- 值得程序员和设计师关注的微信公众号
- HDU 2602 Bone Collector(01背包)
- LeetCode(52) N-Queens II
- 学习新技术的10个建议
- 毛泽东体育文化:文明其精神,野蛮其体魄
- VTK利用点云高程渲染点云(方法二:vtkUnstructuredGrid)
- 矩阵转置(数据结构课本内容)