题解:P11232 [CSP-S 2024] 超速检测(民间数据)

· · 题解

首先我们有两种情况:

设ne[i]为在第i号车后面第一个测速仪的编号

不难得知,这两种情况超速的区间是连续的一段区间,而且要么起点为ne[i],要么终点为m。我们就用f[i]来表示第i辆车超速的情况

f[i]=\begin{cases} 0,不超速\\ 1,起点为ne[i]\\ 2,终点为m\\ 3,起点为ne[i]且终点为m\\ \end{cases}

对于不超速和起点为ne[i]且终点为m的情况,懂的都懂...

对于另外两种情况,我们可以二分枚举长度(赛时我是暴力枚举的...冤...)

这样我们就可以把这n个个点转化为一些区间,然后问题就变成了选一些点,使每个区间内都至少有一个点,接下来就可贪心求解

code:

真是又臭又长

#include<bits/stdc++.h>
using namespace std;
struct node{
    int d,v,a;
}p[100005];//车
struct fw{
    int a,b;
}q[100005];//区间
int n,m,l,v,a[100005],sum,f[100005],ne[100005],cnt;
bool cmp(node x,node y){
    return x.d<y.d;
}
bool cmp1(fw x,fw y){
    if(x.b==y.b) return x.a<y.a;
    return x.b<y.b;
}
void solve(){
    cin>>n>>m>>l>>v;
    for(int i=1;i<=n;i++) cin>>p[i].d>>p[i].v>>p[i].a;
    for(int i=1;i<=m;i++) cin>>a[i];
    int now=1;
    cnt=0;
    sum=0;
    sort(p+1,p+n+1,cmp);
    // sort(a+1,a+m+1);
    memset(f,0,sizeof f);
    memset(ne,0,sizeof ne);
    for(int i=1;i<=n;i++){
        //算出f[i]
        while(a[now]<p[i].d&&now<=m) now++;
        if(now>m) break;
        ne[i]=now;
        if(sqrt(p[i].v*p[i].v+2*p[i].a*(a[now]-p[i].d))>v){
            sum++;
            f[i]=1;
        }
        if(sqrt(p[i].v*p[i].v+2*p[i].a*(a[m]-p[i].d))>v){
            if(f[i]==1) f[i]=3;
            else{
                sum++;
                f[i]=2;
            }
        }
        //算出f[i]

        //计算出区间
        if(!f[i]) continue;
        if(f[i]==3){
            q[++cnt]={ne[i],m};
            continue;
        }
        if(f[i]==1){
            int l=1,r=m-ne[i]+1,sum=1;
            while(l<=r){
                int mid=(l+r)/2;
                if(sqrt(p[i].v*p[i].v+2*p[i].a*(a[ne[i]+mid-1]-p[i].d))>v){
                    l=mid+1;
                    sum=mid;
                }else r=mid-1;
            }
            q[++cnt]={ne[i],ne[i]+sum-1};
            continue;
        }
        int l=1,r=m-ne[i]+1,sum=1;
        while(l<=r){
            int mid=(l+r)/2;
            if(sqrt(p[i].v*p[i].v+2*p[i].a*(a[m-mid+1]-p[i].d))>v){
                l=mid+1;
                sum=mid;
            }else r=mid-1;
        }
        q[++cnt]={m-sum+1,m};
        //计算出区间
    }
    cout<<sum<<" ";
    if(cnt==0){
        cout<<m<<"\n";
        return ;
    }
    //线段覆盖
    sort(q+1,q+cnt+1,cmp1);
//  for(int i=1;i<=cnt;i++) cout<<q[i].a<<" "<<q[i].b<<"\n";
    int nowmax=q[1].b,ans=1;
    for(int i=2;i<=cnt;i++){
        if(q[i].a<=nowmax&&q[i].b>=nowmax){
            continue;
        }
        ans++;
        nowmax=q[i].b;
    }
    cout<<m-ans<<"\n";
    //线段覆盖
    return ;
}
signed main(){
//  freopen("detect.in","r",stdin);
//  freopen("detect.out","w",stdout);
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
/*
1
5 5 15 3
0 3 0
12 4 0
1 1 4
5 5 -2
6 4 -4
2 5 8 9 15

3 3


1
10 10 150307 247
62978 968 -5
51700 883 -3
40408 564 -1
60366 870 -41
35943 797 -7
47476 595 -11
59020 672 -10
81024 964 -11
80637 695 -5
66114 874 -1
46720 48066 57373 60882 68842 72773 82820 116695 122159 150307

10 7
*/