Meteor

Meteor
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
->解题思路:
求出每个Meteor在Frame中起始点与终点,进而找某一时刻,使此时刻在Frame中的Meteor最多。需要注意的使,如果在这一时刻同时有起点和终点,那么应该先减去终点再加上起点,思考一下便可知。也就是说,从头到尾扫描,遇到左端点加一,遇到右端点减一。
//通常我们都会采用实数计算,大佬们一般都会利用最小公倍数来避免实数的运算。通过乘1~10的最小公倍数来达到消除小数的目的。
菜鸟的代码:

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
const int N=1e5+10;
const long long int INF=1e9;
struct time
{ 
	double t;
	bool pos;//0为左边,1为右边 
	bool operator < (const time&x) const 
	{///重载小于号 
		if(t==x.t)  return pos<x.pos;
		else        return t<x.t;
	}
 } ;
time a[N*2];
void check(int x,int x1,int w,double &t0,double &t1)
{/计算起始点与终点 
	if(x1==0)
	{
		if(x<=0||x>=w)  t1=t0-1;
	}
	else if(x1>0)
	{
		t0=max(t0,-(double)x/x1);
		t1=min(t1,-(double)(w-x)/x1);
	}
	else
	{
		t0=max(t0,(double)(w-x)/x1);
		t1=min(t1,-(double)x/x1);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,h,w,x,x1,y,y1,ans,res,cnt;
		ans=0,res=0,cnt=0;
		scanf("%d %d %d",&w,&h,&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d %d %d %d",&x,&y,&x1,&y1);
			double t0=-INF,t1=INF;
			check(x,x1,w,t0,t1);
			check(y,y1,h,t0,t1);
			if(t1>t0)
			{
				a[cnt++]=(time){t0,true};
				a[cnt++]=(time){t1,false};
			}
		}
		sort(a,a+cnt);
		for(int i=0;i<cnt;i++)
		{
			if(a[i].pos)
			   ans=max(ans,++res);
			else
			    res--;
		}
		printf("%d\n",ans);
	}
	return 0;
}
---------------------------------------------------------------------
大佬的代码如下:
---------------------------------------------------------------------
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
const int N=1e5+10;
const long long int INF=1e9;
const double M=3600;
struct time
{ 
	double t;
	bool pos;//0为左边,1为右边 
	bool operator < (const time&x) const 
	{///重载小于号 
		if(t==x.t)  return pos<x.pos;
		else        return t<x.t;
	}
 } ;
time a[N*2];
void check(int x,int x1,int w,double &t0,double &t1)
{/计算起始点与终点 
	if(x1==0)
	{
		if(x<=0||x>=w)  t1=t0-1;
	}
	else if(x1>0)
	{
		t0=max(t0,-x*M/x1);
		t1=min(t1,(w-x)*M/x1);
	}
	else
	{
		t0=max(t0,M*(w-x)/x1);
		t1=min(t1,-x*M/x1);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,h,w,x,x1,y,y1,ans,res,cnt;
		ans=0,res=0,cnt=0;
		scanf("%d %d %d",&w,&h,&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d %d %d %d",&x,&y,&x1,&y1);
			double t0=-INF,t1=INF;
			check(x,x1,w,t0,t1);
			check(y,y1,h,t0,t1);
			if(t1>t0)
			{
				a[cnt++]=(time){t0,true};
				a[cnt++]=(time){t1,false};
			}
		}
		sort(a,a+cnt);
		for(int i=0;i<cnt;i++)
		{
			if(a[i].pos)
			   ans=max(ans,++res);
			else
			    res--;
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值