题解:P17902 白箱

· · 题解

提供一个比较奇怪的思路。

发现如果一集想要在规定时间内完成,又因为必须做完一集才可以做下一集,那么它一定会在它播出之前占据一个整块时间,那么在它的截止日期之内,它在哪准备都是不重要的。

准确来说,一定存在一种最优解,使得各个集按顺序排列,就是说,我先做第一集,再做第二集,再做第三集,以此类推。

所以我们考虑,一开始我们让所有的集数都只占据最少的时间,这样也顺便检查一下是否合法。如果合法,贪心的想,我们需要将 q 值大的优先向右拓展,但是这个拓展是有限制的,首先是题目中 r_i 的限制,其次,如果因为我的拓展而使得有的集没法完成,这也是不行的,所以我们需要维护,一个集后面的每一个集中开始时间离它的最晚开始时间的最小时间差,这是我们当前拓展的另一个限制,有点猎奇了,复杂度 O(n \log n)。

:::info[参考代码]

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N=1e5+5;
const int Inf=1e9+7;

int T,n,s,ti,ans;
int L[N],R[N],sta[N];

int dl[N];

struct Q{
    int x;
    int id;
}q[N];

bool b[N];

priority_queue<int,vector<int>,greater<int>> pq;

bool cmp(Q x,Q y){
    return x.x > y.x;
}

struct Tr{
    int l;
    int r;
    int dat;
    int lazy;
}t[N*4];

void pushup(int p){
    t[p].dat=min(t[p*2].dat,t[p*2+1].dat);
}

void build(int p,int l,int r){
    t[p].l=l;t[p].r=r;
    t[p].lazy=0;
    if(l==r){
        t[p].dat=dl[l]-sta[l];
        return ;
    }

    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    pushup(p);
}

void pushdown(int p){
    if(t[p].lazy){
        t[p*2].dat+=t[p].lazy;
        t[p*2+1].dat+=t[p].lazy;
        t[p*2].lazy+=t[p].lazy;
        t[p*2+1].lazy+=t[p].lazy;
        t[p].lazy=0;
    } 
}

void upd(int p,int l,int r,int k){
    if(t[p].l>=l &&  t[p].r<=r){
        t[p].dat+=k;
        t[p].lazy+=k;
        return ;
    }

    pushdown(p);
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid) upd(p*2,l,r,k);
    if(r>mid) upd(p*2+1,l,r,k);
    pushup(p);
}

int qry(int p,int l,int r){
    if(t[p].l>=l && t[p].r<=r)
        return t[p].dat;

    pushdown(p);
    int res=Inf;
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid) res=min(res,qry(p*2,l,r));
    if(r>mid) res=min(res,qry(p*2+1,l,r));
    return res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>T;

    while(T--){

        ans=0;

        cin>>n>>s>>ti;

        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            q[i]={x,i};
            //pq.push(q[i],i);

        }

        for(int i=1;i<=n;i++){
            cin>>L[i]>>R[i];
            ans=ans+q[i].x*L[i];
        }

        sort(q+1,q+n+1,cmp);

        for(int i=1;i<=n;i++)
            dl[i]=s+(i-1)*(ti+1)-L[i];



        sta[1]=L[1];
        dl[1]=s;

        for(int i=2;i<=n;i++){

            dl[i]=s+(i-1)*ti;
            sta[i]=sta[i-1]+L[i];

            //pq.push(dl[i]-sta[i]);
        }

        bool f=0;

        for(int i=1;i<=n;i++){
            if( sta[i] > dl[i] ){
                f=1;
                break;
            }
        }

        build(1,1,n);

        //cout<<"QwQ"<<'\n';

        if( f ){
            cout<<"Bansakutsukita!"<<'\n';
            continue;
        }

        for(int i=1;i<=n;i++){

            int id=q[i].id;
            int rst=qry(1,id,n);

            if(rst==0) continue;


            int res=min(rst,R[id]-L[id]);
            ans=ans+q[i].x*res;

            upd(1,id,n,-res);

        }

        cout<<ans<<'\n';

    }



    return 0;
}

:::