40tps!调了5天了,悬关求助
查看原帖
40tps!调了5天了,悬关求助
615236
FF_pigeon楼主2024/8/9 21:08
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 10;
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll readll(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
ll n, d, k;
struct node{
	ll w, pos;
}a[N]; 
deque<ll> q;
int l, r;
ll f[N];
bool check( int g ){
	for( int i = 1 ; i <= n ; ++i )f[i] = -0x7f7f7f7f7f;
	f[0] = 0;
	ll str = 1;
	ll L = max(str,d-g);
	ll R = d+g;
	ll ans = -1;
	int j = 0;
	for( int i = 1 ; i <= n ; ++i ){
		while( !q.empty() && a[q.front()].pos < a[i].pos - R )q.pop_front();
		while( a[j].pos <= a[i].pos - L && j < i ){
			while( !q.empty() && f[q.back()] <= f[j] )q.pop_back();
			q.push_back(j);
			++j;
		}
		f[i] = f[q.front()] + a[i].w;
		if( f[i] >= k )return true;
	}
	return false;
}
int erfen( int l , int r){
	if( l == r )return l;
	int mid = (l + r) >> 1;
	if( check(mid) ){
		return erfen(l,mid);
	}
	else return erfen(mid+1,r);
}
int main(){
	n = readll();d = readll();k = readll();
	ll tot = 0;
	for( int i = 1 ; i <= n ; ++i ){
		a[i].pos = readll();a[i].w = readll();
		if( a[i].w > 0 )tot += a[i].w;
	}
	if( tot < k ){
		cout << -1 << "\n";
		return 0;
	}
	l = 1 , r = max(a[n].pos,d);
	int ans = erfen(l,r);
	cout << ans << '\n';
	return 0;
}

AC的点是#1,#2,#4,#5.

2024/8/9 21:08
加载中...