60分求助
查看原帖
60分求助
531650
emmc楼主2023/8/12 16:30

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+500;
int n,d,k;
int dp[N];
int f[N];
int v[N];
int q[N];
int h,t,p;

int check(int g){
	for(int i=0;i<=n;i++) dp[i]=-1e18;
	dp[0]=0LL;
	h=1;t=1;p=0;
	int b,s;
	for(int i=1;i<=n;i++){
		s=f[i]-d-g;
		b=f[i]-max(d-g,(int)1);
		while(f[p]<s and p<i) p++;
		while((f[p]>=s and f[p]<=b) and p<i){
			while(dp[q[h]]<=dp[p] and h<=t) t--;
			q[++t]=p;
			p++;
		}
		while(!(f[q[h]]>=s and f[q[h]]<=b) and h<=t) h++;
		if(h<=t) dp[i]=dp[q[h]]+v[i];
		if(dp[i]>=k) return true;
	}
	return false;
}

signed main(){
	//freopen("a.out","w",stdout);
	ios::sync_with_stdio(false);
	cin>>n>>d>>k;
	for(int i=1;i<=n;i++){
		cin>>f[i]>>v[i];
	}
	//cout<<check(24)<<endl;
	int l,r,mid;
	l=1;r=N-1;mid=(l+r+1)/2;
	while(l<r){
		mid=(l+r)/2;
		if(check(mid)) r=mid;
		else l=mid+1;
	}
//	for(int i=1;i<=n;i++){
//		if(i==24) cout<<"f";
//		cout<<check(i)<<' ';
//	}
	if(!check(l) and !check(r)){
		cout<<-1<<endl;
	}
	else cout<<l<<endl;
	return 0;
	
}
2023/8/12 16:30
加载中...