"Circular process" loop transport

Title Description

Here Insert Picture Description

answer

A very obvious conclusion, if we want to calculate ij (i <j) answer:

  • in case i j &gt; n 2 i-j&gt;\frac{n}{2} Must choose n ( i j ) = j + n i . n-(i-j)=j+n-i. At this time ( j + n ) i &lt; n 2 (j+n)-i&lt;\frac{n}{2} In this case amounts to solving j + n j + n and i i answer.
  • Otherwise, select i j . i-j.

At this point, we can double the range, the distance is solved n 2 \frac{n}{2} Internal, a [ j ] j a[j]-j 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;
}

Guess you like

Origin blog.csdn.net/Ronaldo7_ZYB/article/details/92642637