UVA 1398 1398 – Meteor

Time limit: 3.000 seconds

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 example, 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. UVA 1398
1398 – Meteor

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 wand h (1wh100, 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 xiyiai , and bi ; (xiyi) is the starting point pi and (aibi)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
题目大意是:给你一个矩形照相机,还有n个流星的初始位置和速度,求能照到流星最多的时刻,注意边界上的点不会被照到。
输入格式;第一行输入测试案例数T;每组数据的第一行为两个整数w和h;第二行为流星的个数n;以下n行每行用4个整数xi,yi,ai,bi来描述一个流星,其中(xi,yi)是初始位置,(ai,bi)是速度,ai和bi不同时为0.
输出格式:对于每组数据,输出能照到的流星个数的最大值。
解题思路:(1)、我们将每个流星在矩形照相机中出现的时间段,转换成 n个区间,然后求出一个数t,使得包含它的区间数最多;
(2)、进行排序
(3)、我们用一个扫描先从区间的左端向右端移动,当遇到左端点时就加1,遇到右端点时就减1;
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn=100010;
 5 void fun(int x,int a,int w,double &L,double &R)
 6 {
 7     if(a==0)
 8         {if(x<=0||x>=w) R=L-1;}
 9     else
10     {
11         if(a>0)
12         {
13             L=max(L,-(double)x/a);
14             R=min(R,double(w-x)/a);
15         }
16         else
17             {
18                L=max(L,double(w-x)/a);
19                R=min(R,-(double)x/a);
20             }
21     }
22 }
23 struct Event
24 {
25     double x;
26     int type;
27     bool operator < (const Event &a)const
28     {
29         if(x!=a.x)
30            return x<a.x;
31         else
32           return type>a.type;
33     }
34 }events[maxn*2];
35 int main()
36 {
37     int T;
38     cin>>T;
39     while(T--)
40     {
41         int w,h,n,e=0;
42         cin>>w>>h>>n;
43         for(int i=0;i<n;i++)
44         {
45             int x,y,a,b;
46             cin>>x>>y>>a>>b;
47             double L=0,R=1e9;
48             fun(x,a,w,L,R);
49             fun(y,b,h,L,R);
50             if(R>L)
51             {
52                events[e++]={L,0};
53                events[e++]={R,1};
54             }
55          }
56          sort(events,events+e);
57          int cnt=0,ans=0;
58          for(int i=0;i<e;i++)
59          {
60             if(events[i].type==0)
61                ans=max(ans,++cnt);
62          else
63             cnt--;
64          }
65           cout<<ans<<endl;
66     }
67     return 0;
68 }
View Code