Connecting...

[CSP-S 2024] 超速检测 - 洛谷 解题报告

[CSP-S 2024] 超速检测 - 洛谷 解题报告

思路很清晰,就是解决第一问的时候把超速区间提取出来,然后按右端点为第一关键字排序,再贪心。

我跳的坑点:

  • 注意区间的开闭对答案有一定的影响,有些区间端点算出来是整数,但是如果开但是我们当成闭会有问题,我们对左端点(算出来的)加上偏移量,对算出来的右端点减去偏移量,有些不需要加减,自己看好。
  • 贪心的时候注意不要多加少加了,读者可以自己手模或yy

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
#define db double
int n,m,tot;
db L,V,d[N],v[N],a[N],p[N],eps=1e-8;
struct interval{
	db s,t;
}iv[N]; 
inline bool cmp(interval z,interval x){
	if(z.t==x.t)return z.s<x.s;
	return z.t<x.t;
}
inline void init()
{	
	tot=0;
	cin>>n>>m>>L>>V;
	for(int i=1;i<=n;++i)cin>>d[i]>>v[i]>>a[i];
	for(int i=1;i<=m;++i)cin>>p[i];
}
inline int find(db x){//find the index of p_i which satisfy p_j>=p_i>=x ,(p_j>x)
	int l=1,r=m,ans=-1;
	while(l<=r){
		int mid=l+r>>1;
		if(p[mid]>=x)ans=mid,r=mid-1;
		else l=mid+1;
	}return ans;
}
inline int find1(db x){//find the index of p_i which satisfy p_j<=p_i<=x ,(p_j<x)
	int l=1,r=m,ans=-1;
	while(l<=r){
		int mid=l+r>>1;
		if(p[mid]<=x) ans=mid,l=mid+1;
		else r=mid-1;
	}return ans;
}
inline void judge(){
	int cnt=0;
	for(int i=1;i<=n;++i){
		if(a[i]==0)
			if(v[i]>V&&p[m]>=d[i]){
				iv[++tot]={d[i],L};
				cnt++;
			}
		if(a[i]>0){
			if(v[i]<=V&&d[i]<=p[m] && v[i]*v[i]+2*a[i]*(p[m]-d[i])>V*V){
				cnt++;
				iv[++tot]={(V*V-v[i]*v[i])/(2*a[i])+d[i]+eps,L};
			}
			if(v[i]>V&&d[i]<=p[m]){
				cnt++;
				iv[++tot]={d[i],L};
			}
		}
		if(a[i]<0)
			if(v[i]>V && find(d[i])!=-1 && (p[find(d[i])]-d[i])*2*a[i]>=(V*V-v[i]*v[i])){
				cnt++;
				iv[++tot]={d[i],d[i]+(V*V-v[i]*v[i])/(2*a[i])-eps};
			}
	}
	cout<<cnt<<" ";
}
inline void shut(){
	sort(iv+1,iv+1+tot,cmp);
	int cnt=0,nw=1;
	db lst;
	while(nw<=tot){
		lst=p[find1(iv[nw].t)];
		cnt++;
		while(nw+1<=tot){
			nw++;
			if(iv[nw].s>lst){
				break;
			}
		}
		if(nw==tot&&lst>=iv[nw].s)break;
	}
	cout<<m-cnt<<"\n";
}
signed main(){
//	freopen("detect.in","r",stdin);
//	freopen("detect.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0);
	int t;cin>>t;
	while(t--){
		init();
		judge();
		shut();
	}
	return 0;
}
posted @ 2025-04-06 15:45  余亦宸  阅读(50)  评论(0)    收藏  举报