Seoul 2007 / UVa 1398 Meteor (排序&线性扫描)
来源:互联网 发布:zs什么意思网络用语 编辑:程序博客网 时间:2024/06/05 10:38
1398 - Meteor
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4144
The famous Korean internet company nhn has provided an internet-based photo service which allows The famous Korean internet company users to directly take a photo of an astronomical phenomenon in space by controlling a high-performance telescope owned by nhn. A few days later, a meteoric shower, known as the biggest one in this century, is expected. nhn has announced a photo competition which awards the user who takes a photo containing as many meteors as possible by using the photo service. For this competition, nhn provides the information on the trajectories of the meteors at their web page in advance. The best way to win is to compute the moment (the time) at which the telescope can catch the maximum number of meteors.
You have n meteors, each moving in uniform linear motion; the meteor mi moves along the trajectory pi +t×vi over time t , where t is a non-negative real value, pi is the starting point of mi and vi is the velocity of mi . The point pi = (xi, yi) is represented by X -coordinate xi and Y -coordinate yi in the (X,Y) -plane, and the velocity vi = (ai, bi) is a non-zero vector with two components ai and bi in the (X, Y) -plane. For example, if pi = (1, 3) and vi = (-2, 5) , then the meteor mi will be at the position (0, 5.5) at time t = 0.5 because pi + t×vi = (1, 3) + 0.5×(-2, 5) = (0, 5.5) . The telescope has a rectangular frame with the lower-left corner (0, 0) and the upper-right corner (w, h) . Refer to Figure 1. A meteor is said to be in the telescope frame if the meteor is in the interior of the frame (not on the boundary of the frame). For exam! ple, in Figure 1, p2, p3, p4 , and p5 cannot be taken by the telescope at any time because they do not pass the interior of the frame at all. You need to compute a time at which the number of meteors in the frame of the telescope is maximized, and then output the maximum number of meteors.
Input
Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers w and h (1w, h100, 000) , the width and height of the telescope frame, which are separated by single space. The second line contains an integer n , the number of input points (meteors), 1n100, 000. Each of the next n lines contain four integers xi, yi, ai , and bi ; (xi, yi) is the starting point pi and(ai, bi) is the nonzero velocity vector vi of the i -th meteor; xi and yi are integer values between -200,000 and 200,000, and ai and bi are integer values between -10 and 10. Note that at least one of ai and bi is not zero. These four values are separated by single spaces. We assume that all starting points pi are distinct.
Output
Your program is to write to standard output. Print the maximum number of meteors which can be in the telescope frame at some moment.
Sample Input
2 4 2 2 -1 1 1 -1 5 2 -1 -1 13 6 7 3 -2 1 3 6 9 -2 -1 8 0 -1 -1 7 6 10 0 11 -2 2 1 -2 4 6 -1 3 2 -5 -1
Sample Output
1 2
1. 路径?时间?怎么化繁为简?——对每颗可以被望远镜捕捉到(进入矩形内部)的流星,记录其进入时间和离开时间。
2. 如何计算进出时间?——2个维度分开判断,进入时间取最大值,离开时间取最小值。
3. 怎么判断某一时刻捕捉到的流星数目最多?——排序&扫描:对所有进入和离开的时间进行排序,对于时间相同的点,把离开时间放前面。
4. 扫描?——我把它叫做减增调整法,当有新的扫描事件发生时(即扫描了一个进入时间或离开时间),就减/增流星数目,并比较当前最大值。
完整代码:
/*0.122s*/#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100010;struct Event{int x;int type;bool operator < (const Event& a) const{return x < a.x || (x == a.x && type > a.type); // 先处理离开时间}} events[maxn << 1];// 0<x+at<wvoid update(int x, int a, 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); //注意到-10 <= ai,bi <= 10,可以通过乘上2~10的最小公倍数来避免实数运算R = min(R, (w - x) * 2520 / a);}else{L = max(L, (w - x) * 2520 / a);R = min(R, -x * 2520 / a);}}int main(void){int T;scanf("%d", &T);while (T--){int w, h, n, e = 0;scanf("%d%d%d", &w, &h, &n);for (int i = 0; i < n; i++){int x, y, a, b;scanf("%d%d%d%d", &x, &y, &a, &b);// 0<x+at<w, 0<y+bt<h, t>=0int L = 0, R = 1000000000;update(x, a, w, L, R);update(y, b, h, L, R);//2个维度分开判断if (R > L){events[e++] = (Event){L, 0};events[e++] = (Event){R, 1};}}sort(events, events + e);int cnt = 0, ans = 0;for (int i = 0; i < e; i++){if (events[i].type == 0) ans = max(ans, ++cnt); // 由于要保证ans的取值正确,所以当两时间相同时,把离开时间放前面else cnt--;}printf("%d\n", ans);}return 0;}
- Seoul 2007 / UVa 1398 Meteor (排序&线性扫描)
- Meteor Seoul 2007
- UVA 1398 - Meteor(排序+扫描方法+几何)
- UVA 1398 Meteor(扫描线)
- Seoul 2007 Meteor 流星 ,LA 3905
- UVa 1398 Meteor 解题报告(扫描线)
- uva 1398 - Meteor
- uva 1398 - Meteor
- UVA 1398 - Meteor
- UVA 1398 - Meteor
- UVA之1398 - Meteor
- UVA 1398 Meteor
- Uva---1398Meteor
- UVA - 1398 Meteor
- LA 3905 Meteor (排序+扫描法)
- Asia - Seoul - 2007/2008 UVA 3902 Network
- uva 1398 - Meteor 模拟 99
- uva 1398 - Meteor(贪心)
- 直接双击启动tomcat中的startup.bat闪退
- 规则引擎如何操作内存表学习
- 冒泡排序
- JSON帮助类
- Linux中如何退出man命令
- Seoul 2007 / UVa 1398 Meteor (排序&线性扫描)
- Tomcat 的 SSL 配置
- 图片资源添加出现问题: No resource found that matches the given name
- logcat 命令小结
- Cocos2d-x +Android + Windows 7环境配置
- Struts2.3+Spring2.5.6+JPA(Hibernate3)项目移植入WebLogic12容器,并整合入MyEclipse10 图文详解
- java中queue的使用
- v4l2 编程接口(二) — driver
- android_广播BroadCaster