题解:P11232 [CSP-S 2024] 超速检测

· · 题解

题解区有二分做法了,简单说一下我的非二分做法吧。

引理:在道路上一辆车要么完全不超速,要么在一个连续区间内超速。

证明:a_i=0 时没有加速度,证明显然,a_i \gt 0 时,只要瞬时速度超过 V,因为加速度为正,速度会不断增加,因此仍会一直超速,当 a_i \lt 0 时,速度会不断减慢,在速度降低到 V 以下前都是超速的。

下面定义辅助函数 \operatorname{pos}(x,spd) 表示第 x 辆车速度达到 spd 时的坐标。

考虑处理出每辆车的超速区间,当 a_i=0 时,如果 v_i \gt V,超速区间是 [d_i, L],否则为空集;当 a_i \gt 0 时,设 y=\max(\operatorname{pos}(x, V)+\text{eps}, d_i),超速区间为 [y,L](注意这里 \text{eps} 很重要,因为速度 =V 不是超速);当 a_i \lt 0 时,如果初速度 v_i \leq V 则超速区间为空集,否则设 y=\min(\operatorname{pos}(x,0), \operatorname{pos}(x,V)-\text{eps},L),超速区间为 [d_i,y](注意这里 \operatorname{pos}(x,0) 没有 \text{eps},因为速度为 0 时也能被拍到)。

按上述方法预处理出超速区间,之后对于每一辆车若超速区间内有至少一个探头就会被抓拍,第一问解决,考虑第二问,将问题形式化一下:我们有 N 个区间(车的超速区间)和 M 个点(探头),我们希望选择最少的点使得每一个包含至少一个点的区间都至少包含一个被选中的点,发现什么了吗?这是经典的线段覆盖问题,考虑按区间的右端点升序排序,对于每一个区间,若当前区间内没有选择的点,考虑选择区间内最右侧的点,可以通过反证法证明。

这里需要实现区间求和和单点 +1,可以使用经典数据结构维护,例如树状数组/线段树,笔者使用的是树状数组,复杂度为 O(TN \log L),常数很小。

小细节:d_i 可能等于 0,为了防止下标越界可以在树状数组中给每一个下标加一再操作。

参考代码:

#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;

#define LD long double
const int maxn=1e5+10,maxL=1e6+10;
const LD eps=1e-9;
int T,n,m,L,V,det[maxL],nxt[maxL],lst,ans1,pre[maxL],ans2;
struct car{ int pos,spd,inc; } cars[maxn];
struct limit{
    int l,r; 
    limit(LD L, LD R){ l=ceill(L); r=floorl(R); }
    bool operator<(const limit other)const{ return r!=other.r?r<other.r:l>other.l;; }
}; vector<limit> limits;

bool overspeed(LD spd){ return spd>V; }
LD speed(car x, int p){ return sqrtl(x.spd*x.spd+2*x.inc*(p-x.pos)); }
LD pos(car x, LD spd){ return x.pos+(LD)(spd*spd-x.spd*x.spd)/2/x.inc; }

bool test(car x){
    if(x.pos>lst)return false;
    if(x.inc>=0)return overspeed(speed(x, lst));
    if(pos(x,0)<nxt[x.pos])return false;
    return overspeed(speed(x, nxt[x.pos]));
}
void add(car x){
    if(x.inc==0)limits.push_back({x.pos, L});
    else if(x.inc>0)limits.push_back({max(pos(x, V)+eps, (LD)x.pos), L});
    else{
        LD y=min(pos(x, V)-eps,min((LD)L, pos(x,0)));
        limits.push_back({x.pos, y});
    }
}

int tree[maxL];
int lowbit(int x){ return x&-x; }
void clear(){ for(int i=0;i<=L+1;i++)tree[i]=0; }
void add(int p, int v){ for(;p<=L+1;p+=lowbit(p))tree[p]+=v; }
int query(int p){ int ans=0; for(;p;p-=lowbit(p))ans+=tree[p]; return ans; }

int main(){
    freopen("detect.in", "r", stdin);
    freopen("detect.out", "w", stdout);

    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d",&n,&m,&L,&V); ans1=lst=0; ans2=m; limits.clear(); clear();
        for(int i=0;i<=L+1;i++)det[i]=0;
        for(int i=1;i<=n;i++)scanf("%d%d%d",&cars[i].pos,&cars[i].spd,&cars[i].inc);
        for(int i=1;i<=m;i++){
            int p; scanf("%d",&p);
            det[p]=1; lst=max(lst, p);
        }

        for(int i=L;i>=0;i--){
            if(det[i])nxt[i]=i;
            else nxt[i]=nxt[i+1];
        }
        pre[0]=det[0]-1;
        for(int i=1;i<=L;i++){
            if(det[i])pre[i]=i;
            else pre[i]=pre[i-1];
        }

        for(int i=1;i<=n;i++)if(test(cars[i]))ans1++,add(cars[i]);
        sort(limits.begin(), limits.end());
        for(limit x:limits)if(query(x.r+1)<=query(x.l)){
            //cout<<x.l<<' '<<x.r<<endl;
            int pos=pre[x.r]; add(pos+1,1);
            ans2--;
        }

        printf("%d %d\n",ans1,ans2);
    }
}