题意:
给出流星的初始位置和速度,求某个时刻框内流星的数目最多
思路:
求出流星在框内出现的时间段,然后对各时间段扫描,遇到左端点就累加,遇到右端点表示流星出框,集合交集最多的地方就是所求
ps:过了很就以后补的博客,可能有错
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int w, h, n;
struct star {
double x;
int type;
bool operator <(const star &st) const{
if(x != st.x) return x < st.x;
else return type > st.type;//使相同端点不重和
}
}s[100005*2];
int x, y, a, b;
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);//到达框内的时间,如果已经在框内,自然为0
R = min(R, (double)(w-x)/a);//出框时间
}
else {
L = max(L, (double)(w-x)/a);
R = min(R, -(double)(x)/a);
}
}
int main() {
int kase;
scanf("%d", &kase);
while(kase--) {
scanf("%d%d%d", &w, &h, &n);
int e=0;
for(int i=0; i<n; i++) {
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(R>L) {//出现在框内
s[e++] = (star){L,0};
s[e++] = (star){R,1};
}
}
sort(s, s+e);
int cnt=0, ans=0;
for(int i=0; i<e; i++) {
if(s[i].type == 0) {
cnt++;//遇到左端点就累加
if(ans < cnt) ans = cnt;
}
else cnt--;
}
printf("%d\n", ans);
}
return 0;
}