AcWing 289. 环路运输

5 篇文章 0 订阅

传送门

思路:

一个环路上的问题,考虑拆环为链然后复制一倍接在后面。那么对于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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值