Title Description
answer
A very obvious conclusion, if we want to calculate ij (i <j) answer:
- in case Must choose At this time In this case amounts to solving and answer.
- Otherwise, select
At this point, we can double the range, the distance is solved Internal, value of the maximum points.
At this point we can use the priority queue to maintain, it is still very simple. '
So ring in solving the problem, we can also consider extending the method to double to solve the problem.
code show as below:
#include <bits/stdc++.h>
using namespace std;
const int N = 2000000;
int n, h = 1, t = 0, ans = 0;
int a[N], q[N];
inline int read(void)
{
int s = 0, w = 1; char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') s = s*10+c-48, c = getchar();
return s * w;
}
int main(void)
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
n = read();
for (int i=1;i<=n;++i) a[i] = a[i+n] = read();
for (int i=1;i<=n*2;++i)
{
while (h <= t && i-q[h] > n/2) h ++;
ans = max(ans,a[i]+i+a[q[h]]-q[h]);
while (h <= t && a[i]-i >= a[q[t]]-q[t]) t --;//注意维护a[i]-i的最小值
q[++t] = i;
}
cout << ans <<endl;
return 0;
}