题解:扫描线,题目有意义的在于每个流星在矩阵内部出现的时间段,题目问最多出现多少颗流星,时刻t转化为竖线,若竖线经过的y个时间段, 维护y,并更新ans即可,需要注意,矩阵边框不记录
巧妙的地方在于如何判断一个流星的时间段.利用初中学的向量分解,把x和y分解,并求出他们的时间段,取时间段交集即可
#include <bits/stdc++.h>
using namespace std;
const int mmax = 1e5+5;
struct REG{ //区间
double x;
int ty;
bool operator < (const REG & b)const{
return x < b.x || (x == b.x && ty > b.ty);
}
}p[mmax*2]; //点
void updata(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,(0-x)*1.0/a);
R = min(R,(w-x)*1.0/a);
}
else{
L = max(L,(w-x)*1.0/a);
R = min(R,(0-x)*1.0/a);
}
}
int main(){
int t,w,h,x,y,a,b,n;
cin >> t;
while(t--){
int sum = 0 ,cnt = 0,ans = 0;
scanf("%d%d",&w,&h);
cin >> n;
for(int i = 0 ; i < n ;i++){
scanf("%d%d%d%d",&x,&y,&a,&b);
double L = 0 ,R = 1e9;
updata(x,a,w,L,R);
updata(y,b,h,L,R);
if(L < R){
p[sum++] = (REG){L,0};
p[sum++] = (REG){R,1};
}
}
sort(p,p+sum);
for(int i = 0 ; i < sum ; i++){
if(p[i].ty == 0) { cnt++; ans = max(cnt,ans); }
else { cnt--;}
}
cout << ans << endl;
}
}