救命样例过不去不知道哪里错了
查看原帖
救命样例过不去不知道哪里错了
359883
Grace25楼主2022/8/27 21:25
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=500005;
const long long INF=0x8080808080808080;
long long n,d,k;
long long x[N],s[N];
long long l,r,mid;
long long dp[N];
deque<long long> q;
bool check(long long g){
    long long ls=d-g>0?d-g:0;
    long long rs=d+g;
    for(int i=1;i<=n;i++) dp[i]=-INF;
    dp[0]=0;
    q.clear();
    int las=0;
    for(long long i=1;i<=n;i++){
        while(x[las] < x[i]-rs) las++;
        while(x[i]-ls >= x[las] && x[i]-rs <= x[las] && las<i){
            while(q.size() && (dp[q.back()]<=dp[las] || x[q.back()]<x[i]-rs)) q.pop_back();
            q.push_back(las);
            las++;
        }
        while(q.size() && (x[q.front()])<x[i]-rs) q.pop_front();
        if(q.size()) dp[i]=dp[q.front()]+s[i];
        if(dp[i]>=k) return 1;
    }
    return 0;
}
int main(){
    cin >> n >> d >> k;
    for(long long i=1;i<=n;i++)
    cin >> x[i] >> s[i];
    l=0,r=x[n];
    while(l<r){
        mid=(l+r)/2;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    if(!check(l)){
        cout << "-1";
        return 0;
    }
    cout << l;
    return 0;
}
2022/8/27 21:25
加载中...