环路运输

在一条环形公路旁均匀地分布着N座仓库,编号为1~N,编号为 i 的仓库与编号为 j 的仓库之间的距离定义为 dist(i,j)=min⁡(|i-j|,N-|i-j|),也就是逆时针或顺时针从 i 到 j 中较近的一种。

每座仓库都存有货物,其中编号为 i 的仓库库存量为 Ai。

在 i 和 j 两座仓库之间运送货物需要的代价为 Ai+Aj+dist(i,j)。

求在哪两座仓库之间运送货物需要的代价最大。

输入格式
第一行包含一个整数N。

第二行包含N个整数A1~AN。

输出格式
输出一个整数,表示最大代价。

数据范围
1≤N≤10^6,
1≤Ai≤10^7

输入样例:
5
1 8 6 2 5
输出样例:
15
#include<bits/stdc++.h>

using namespace std;
const int N = 2e6 + 10;
int a[N], n, ans;
deque<int> q;

int main() {
    cin >> n;
    int len = n >> 1;
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]), a[i + n] = a[i];
    q.push_back(1);
    for (int i = 2; i < n + len; i++) {
        while (i - q.back() > len)q.pop_back();
        ans = max(ans, a[i] + a[q.back()] + i - q.back());
        while (!q.empty() && a[q.front()] - q.front() <= a[i] - i)q.pop_front();
        q.push_front(i);
    }
    cout << ans << endl;
    return 0;
}
发布了329 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/104784537