救救孩子 50分WA警告
查看原帖
救救孩子 50分WA警告
38171
DeNeRATe楼主2020/5/25 09:34
//P3957
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>

#define LL long long
#define Cl(X,Y) memset((X),(Y),sizeof(X))
#define FOR(i,A,B) for(LL i=A;i<=B;i++)
#define BOR(i,A,B) for(LL i=A;i>=B;i--)
#define INF 0x3f3f3f3f
using namespace std;
const LL MAXn=5e5+10;

LL DP[MAXn],Head,Tail,L,R,Che;
LL Queue[MAXn],Total,Dis,Limit,Ans;
struct Node {
    LL Loc,Pts;
}House[MAXn];

inline bool Check(LL Temp) {
    Cl(Queue,0); Cl(DP,0); Head=1,Tail=0;
    LL Res=-INF;
    Queue[++Tail]=0;
    FOR(i,1,Total) {
        while(Head<=Tail && House[i].Loc-House[Queue[Head]].Loc>Temp+Dis) Head++;
        if(Head>Tail) DP[i]=-INF;
        else if(House[i].Loc-House[Queue[Head]].Loc<max(1ll,Dis-Temp)) DP[i]=-INF;
        else DP[i]=DP[Queue[Head]]+House[i].Pts;
        Res=max(max(Res,DP[i]),(LL)0);
        while(Head<=Tail && DP[i]>DP[Queue[Tail]]) Tail--;
        Queue[++Tail]=i;
    }
    return Res>=Limit;
}

inline void Work() {
    L=0,R=House[Total].Loc;
    while(L<=R) {
        LL Mid=(L+R)>>1;
        if(Check(Mid)) R=Mid-1,Ans=Mid;
        else L=Mid+1;
    }
    printf("%lld\n",Ans);
}

int main() {
    scanf("%d %d %d",&Total,&Dis,&Limit);
    FOR(i,1,Total) {
        scanf("%lld %lld",&House[i].Loc,&House[i].Pts);
        Che+=(House[i].Pts>0 ? House[i].Pts : 0);
    }
    if(Che<Limit) { printf("-1\n"); exit(0); }
    Work();
    return 0;
}

2020/5/25 09:34
加载中...