题解:CF1901D Yet Another Monster Fight
zhongzhong517 · · 题解
首先,当第一次选择
-
$\therefore x\ge a_j$。 -
$\therefore x-(n-j+1)+1\ge a_j$。 $\therefore x\ge a_j-j+n$。 -
$\therefore x-j+1\ge a_j$。 $\therefore x\ge a_j+j-1
综上,当第一次攻击
所以,最终答案为
区间最大值,用 ST 表实现。
放代码:
#include <bits/stdc++.h>
using namespace std;
int n, a[300005], lg[300005], st1[300005][20], st2[300005][20];
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;
for(int i = 1; i <= n; i++){
st1[i][0] = a[i] - i + n;
st2[i][0] = a[i] + i - 1;
}
for(int len = 1; len <= lg[n]; len++){
for(int i = 1; i + (1 << len) - 1 <= n; i++){
st1[i][len] = max(st1[i][len - 1], st1[i + (1 << len - 1)][len - 1]);
st2[i][len] = max(st2[i][len - 1], st2[i + (1 << len - 1)][len - 1]);
}
}
int minn = 2147483647;
for(int i = 1; i <= n; i++){
int maxn = 0;
int l1 = 1, r1 = i - 1, l2 = i + 1, r2 = n;
if(l1 <= r1){
int len = lg[r1 - l1 + 1];
maxn = max({maxn, st1[l1][len], st1[r1 - (1 << len) + 1][len]});
}
if(l2 <= r2){
int len = lg[r2 - l2 + 1];
maxn = max({maxn, st2[l2][len], st2[r2 - (1 << len) + 1][len]});
}
maxn = max(maxn, a[i]);
minn = min(minn, maxn);
}
cout << minn;
return 0;
}