跳到正文

题解

题解:P11157 【MX-X6-T3】さよならワンダーランド

KingGoat约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年10月3日
收录
2026年8月9日
更新
2026年11月13日
篇幅
约 3 分钟
获赞
2
题目
P11157

版本

  1. v1当前

这种题不就用数据结构维护一下就行了吗?

∵i+j≤n\because i+j \le n ∴j≤n−i\therefore j \le n-i ∵ai≤j≤ai+j\because a_{i} \le j \le a_{i+j} ∴ai≤j\therefore a_{i} \le j ∴ai≤j≤n−i\therefore a_{i} \le j \le n-i

显然,对于每一个 ii,若 ai>n−ia_{i}>n-i 则肯定无解。

然后可以根据上面的不等式算出 jj 的取值范围,此时只需要找到一个 jj,使得 j≤ai+jj \le a_{i+j} 即可。

考虑移项,即找到满足 ai+j−j≥0a_{i+j}-j \ge 0 的 jj。

因为题目只要求找到一个解,所以肯定是找 ai+j−ja_{i+j}-j 最大的那个解,如果此时 ai+j−j<0a_{i+j}-j<0 则无解,否则直接输出这个满足条件的答案即可。

我们发现 ai+j−ja_{i+j}-j 这个东西很不好维护,因为这个 jj 对于不同的 ii 来说是不一样的,所以我们考虑维护 ai+j−(i+j)a_{i+j}-(i+j) 的最大值。那为什么可以这样维护呢?因为 ii 为定值,所以对于原式一起减掉 ii 是不会影响大小关系的,把最大值求出来后加上 ii 就是最大的 ai+j−ja_{i+j}-j。

维护区间最大值和最大值的位置随便用个数据结构维护就行了。

AC Code

C++
#include <bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 3e5 + 5;
int n, a[N];
struct node{
	int val, id;
}dp[N][41];
node query (int l, int r){
	int pos = log2 (r - l + 1);
	if (dp[l][pos].val >= dp[r - (1 << pos) + 1][pos].val) return dp[l][pos];
	else return dp[r - (1 << pos) + 1][pos];
}
signed main (){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i){
		cin >> a[i];
		dp[i][0].val = a[i] - i;
		dp[i][0].id = i;
	}
	for (int j = 1; (1 << j) <= n; ++j){
		for (int i = 1; i + (1 << j) - 1 <= n; ++i){
			if (dp[i][j - 1].val >= dp[i + (1 << (j - 1))][j - 1].val) dp[i][j] = dp[i][j - 1];
			else dp[i][j] = dp[i + (1 << (j - 1))][j - 1];
		}
	}
	for (int i = 1; i <= n; ++i){
		int lt = a[i], rt = n - i;
		if (lt > rt){
			cout << 0 << '\n';
			continue;
		}
		node tmp = query (max (lt + i, 1ll), rt + i);
		int pos = a[tmp.id], posr = tmp.id - i;
		if (pos >= posr) cout << 1 << ' ' << posr << '\n';
		else cout << 0 << '\n';
	}
	return 0;
}
原文
存档评论5 条抓取于