思路:
一个环路上的问题,考虑拆环为链然后复制一倍接在后面。那么对于Ai与Aj,不妨设j<i,如果i-j>N/2则两者距离在新的链上就是i-j,而如果i-j<=N/2那么两者之间的距离就是j+N-i=N-(i-j),而这个值<=N/2,所以二者的距离在新的链上一定不超过N/2。设新的链上两个物品为Ax与Ay,设y<x,可能的答案为Ax+Ay+ x-y=Ax+x+Ay-y,所以我们可以枚举x,用单调队列维护一下Ay-y,其在队列内单调递减,就可以求出答案了。
代码:
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
//#define int LL
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#pragma warning(disable :4996)
const int maxn = 2000100;
const double eps = 1e-6;
const LL MOD = 998244353;
LL N, A[maxn];
deque<LL>q;
void solve()
{
LL ans = -INF;
LL K = N * 2, M = N / 2;
for (int i = 1; i <= N; i++)
A[i + N] = A[i];
q.push_back(1);
for (LL i = 1; i <= K; i++)
{
while (!q.empty() && q.front() + M <= i)
q.pop_front();
ans = max(ans, A[i] + i + A[q.front()] - q.front());
while (!q.empty() && A[q.back()] - q.back() <= A[i] - i)
q.pop_back();
q.push_back(i);
}
cout << ans << endl;
}
int main()
{
IOS;
cin >> N;
for (int i = 1; i <= N; i++)
cin >> A[i];
solve();
return 0;
}