把每个流星进入视野的时间分解成两个事件,进入视野和离开视野,然后把所有的事件按照时间顺序排序,时间一样的事件把离开视野事件排在前面,然后从左到右遍历所有事件,遇到进入视野事件流星数加1,反之减1,维护流星数量的最大值就行了,注意利用最小公倍数来规避浮点数运算。
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 0x7fffffff
#define START 0
#define END 1
typedef unsigned char TYPE;
struct event{
TYPE type;
int time;
};
vector<struct event> event_v;
int _max(int a, int b){
return a>b ? a : b;
}
int _min(int a, int b){
return a<b ? a : b;
}
int _abs(int n){
return n>0 ? n : n*(-1);
}
void update(int x, int y, int a, int b, int w, int h){
if(x<=0 && a<=0)
return;
if(x>=w && a>=0)
return;
if(y<=0 && b<=0)
return;
if(y>=h && b>=0)
return;
int l, r;
struct event eve;
l = 0; r = INF;
if(a == 0){
l = _max(l, 0);
r = _min(r, INF);
}
else if(a > 0){
l = _max(l, (0-x)*2520 / _abs(a));
r = _min(r, (w-x)*2520 / _abs(a));
}
else{
l = _max(l, (x-w)*2520 / _abs(a));
r = _min(r, (x-0)*2520 / _abs(a));
}
if(b == 0){
l = _max(l, 0);
r = _min(r, INF);
}
else if(b > 0){
l = _max(l, (0-y)*2520 / _abs(b));
r = _min(r, (h-y)*2520 / _abs(b));
}
else{
l = _max(l, (y-h)*2520 / _abs(b));
r = _min(r, (y-0)*2520 / _abs(b));
}
if(r > l){
eve.time = l; eve.type = START; event_v.push_back(eve);
eve.time = r; eve.type = END; event_v.push_back(eve);
}
}
int cmp(const void *a, const void *b){
struct event *pa = (struct event*)a;
struct event *pb = (struct event*)b;
if(pa->time != pb->time){
return pa->time - pb->time;
}
else{
if(pa->type == END && pb->type==START)
return -1;
else if(pa->type == START && pb->type == END)
return 1;
else
return 0;
}
}
void func(){
int i, num, max_num;
if(0 == event_v.size()){
printf("0\n");
return;
}
qsort(&(event_v[0]), event_v.size(), sizeof(struct event), cmp);
num = max_num = 0;
for(i=0; i<event_v.size(); i++){
if(START == event_v[i].type)
num++;
else
num--;
if(max_num < num)
max_num = num;
}
printf("%d\n", max_num);
}
int main(void){
int t, w, h, n;
int i;
int x, y, a, b;
//freopen("input.dat", "r", stdin);
scanf("%d", &t);
while(t--){
scanf("%d %d %d", &w, &h, &n);
event_v.clear();
for(i=1; i<=n; i++){
scanf("%d %d %d %d", &x, &y, &a, &b);
update(x, y, a, b, w, h);
}
func();
}
return 0;
}