40分求调
查看原帖
40分求调
1042486
larryia楼主2024/11/21 19:18
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
struct block {
	int x, s;
}b[N];
int d, n, k, dp[N], t, h, q[N], sum;
bool check(int g) {
	t = 0, h = 1;
	memset(q, 0, sizeof q);
	memset(dp, ~0x3f, sizeof dp);
	int l = max(1, d - g), r = d + g;
	int j = 0;
	dp[0] = 0;
	for (int i = 1; i <= n; i++) { 
		for (; j < i && b[j].x < b[i].x - l; j++) {
			if (dp[j] != -0x3f3f3f3f) {
				while (h <= t && dp[j] >= dp[q[t]])
					t--;
				q[++t] = j;
			}
			
		}
		while (h <= t && b[i].x - b[q[h]].x > r) 
			h++;
		if (h <= t)
			dp[i] = dp[q[h]] + b[i].s;
		//cout << dp[i] << endl;
		if (dp[i] >= k) {
			//cout << "======" << endl;
			return true;
		}
	}
	
	//cout << "======" << endl;
	return false;
}
int main() {
	cin >> n >> d >> k;
	for (int i = 1; i <= n; i++) {
		cin >> b[i].x >> b[i].s;	
		if (b[i].s > 0)
		    sum += b[i].s;
	}
	if (sum < k) {
		cout << -1;
		return 0;
	}
	int l = 0, r = 1e9;
	while (l + 1 < r) {
		//cout << l << ' ' << r << endl;
		int mid = (l + r) >> 1;
		if (check(mid)) {
			r = mid;
		} else {
			l = mid;
		}
	}
	
		cout << r;
	
	return 0;
}

@Zhy07 求调

2024/11/21 19:18
加载中...