题解:P11157 【MX-X6-T3】さよならワンダーランド
这种题不就用数据结构维护一下就行了吗?
显然,对于每一个
然后可以根据上面的不等式算出
考虑移项,即找到满足
因为题目只要求找到一个解,所以肯定是找
我们发现
维护区间最大值和最大值的位置随便用个数据结构维护就行了。
AC Code
#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;
}