Meteor

题意:

给出流星的初始位置和速度,求某个时刻框内流星的数目最多

思路:

求出流星在框内出现的时间段,然后对各时间段扫描,遇到左端点就累加,遇到右端点表示流星出框,集合交集最多的地方就是所求

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值