題目大意:給定那個流星的初始位置,和向量速度v(1,2),表示每秒x軸移動一個單位,y軸移動兩個單位。給定相機拍攝的範圍,求相機在什麼時刻拍攝的流星最多,邊界上的不算,相機的左下起始點爲(0,0)右上點的範圍由輸入給定。
題目解析:分析可知,流星的運動軌跡對我們所求的結果沒有意義,我們只需求出每顆流星進入相機拍攝範圍的時間段,得到n個區間段,要求某個時刻拍攝的流星最多,既求所有流星時間段重合最多。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000010
typedef struct node
{
double x;
int type;
}node;
node map[MAX];
int w,h,n;
double max(double x,double y)
{
return x>y?x:y;
}
double min(double x,double y)
{
return x<y?x:y;
}
int cmp(const void *aa,const void *bb)
{
node *a=(node *)aa;
node *b=(node *)bb;
if (a->x!=b->x)
return a->x < b->x ? -1:1;
return b->type-a->type;
}
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)
{
L=max(L,-(double)x/a);
R=min(R,double(w-x)/a);
}
else
{
L=max(L,-(double)(x-w)/a);
R=min(R,-(double)x/a);
}
return ;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int i,j,cnt=0,ans=0,sum=0;
int x,y,a,b;
scanf("%d%d",&w,&h);
scanf("%d",&n);
for (i=0;i<n;i++)
{
double L=0.0,R=1e9;
scanf("%d%d%d%d",&x,&y,&a,&b);
update(x,a,w,L,R);
update(y,b,h,L,R);
if (R>L)
{
map[cnt++]=(node){L,0};
map[cnt++]=(node){R,1};
}
}
qsort(map,cnt,sizeof(node),cmp);
for (i=0;i<cnt;i++)
{
if (map[i].type==0)
{
sum++;
if (ans<sum)
ans=sum;
}
else
{
sum--;
}
}
printf("%d\n",ans);
}
return 0;
}