http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/D
The famous Korean internet company 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 . A few days later, a meteoric shower, known as the biggest one in this century, is expected. 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, 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. Figure 1 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 (1 ≤ w, h ≤ 100, 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), 1 ≤ n ≤ 100, 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
题解:(题目这里也有)网上有刘汝佳的题解
代码:
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100010; struct Event { double x; int type; bool operator < (const Event& a) const { return x < a.x || (x == a.x && type > a.type); } }event[maxn*2]; void update(int x, int a, int w, double& l, double& r) { if(a == 0) { if(x <= 0 || x >= w) r = l - 1;//无解 } else if(a > 0)//0 < x+at < w <=> t > -x/a && t < (w-x)/a { l = max(l, -(double)x/a); r = min(r, (double)(w-x)/a); } else if(a < 0)//0 < x+at < w <=> t < -x/a && t > (w-x)/a { l = max(l, (double)(w-x)/a); r = min(r, -(double)x/a); } } int main() { 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); double l = 0, r = 1e9; update(x, a, w, l, r); update(y, b, h, l, r); if(l < r) { event[e++]= (Event){l, 0}; event[e++]= (Event){r, 1}; } } sort(event, event+e); int cnt = 0; int ans = 0; for(int i = 0; i < e; i++) { if(event[i].type == 0) cnt++; else cnt--; ans = max(ans, cnt); } printf("%d ", ans); } return 0; }
我的题解:
各种条件的判断,注意不要写错>、<
代码:
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int t,n,w,h,ans=1,numOfXing,validNum,valid; double x,y,vx,vy,iT,oT,inTime[100005],outTime[100005]; int main() { scanf("%d",&t); while(t--) { memset(inTime,0,sizeof(inTime)); memset(outTime,0,sizeof(outTime)); validNum=0; ans=0; scanf("%d%d%d",&w,&h,&n); for(int i=1; i<=n; i++) { valid=1; iT=oT=0; scanf("%lf%lf%lf%lf",&x,&y,&vx,&vy); if(y<=0&&vy<=0||y>=h&&vy>=0||x<=0&&vx<=0||x>=w&&vx>=0) valid=0; else if(vy==0) { if((x<=0||x>=w)&&x*vx<=0) iT=min(-x/vx,(w-x)/vx); oT=max(-x/vx,(w-x)/vx); } else if(vx==0) { if((y<=0||y>=h)&&y*vy<=0) iT=min(-y/vy,(h-y)/vy); oT=max(-y/vy,(h-y)/vy); } else { if(x<=0||x>=w||y<=0||y>=h) { if(x*vy/vx<y&& y+(w-x)*vy/vx<h||h+x*vy/vx>y&&y+(w-x)*vy/vx>0) iT=max(min(-x/vx,(w-x)/vx),min(-y/vy,(h-y)/vy)); else valid=0; } oT=min(max(-x/vx,(w-x)/vx),max(-y/vy,(h-y)/vy)); } if(valid) { validNum++; inTime[validNum]=iT; outTime[validNum]=oT; } } sort(inTime+1,inTime+validNum+1); sort(outTime+1,outTime+validNum+1); int j=1; numOfXing=0; for(int i=1; i<=validNum; i++) { numOfXing++; while(outTime[j]<=inTime[i]&&j<=validNum) { j++; numOfXing--; } ans=max(ans,numOfXing); } printf("%d ",ans); } }