Property 1: The title operation is equivalent to moving the front number to the back, and treating it as a bar graph is transferring the height of the front bar to the height of the back bar
Property 2: The final sequence shifted into a monotonically non-declining sequence is optimal, easy to prove that when there is a decline, the answer can be manipulated to make it better or not worse
Property 3: Due to property 2, the tail of the optimal sequence is easily obtained to be the highest, so it can be maintained by the stack, which is a monotonic stack with the highest stack opening
Property 4: for a certain sum sum and the number of occurrences of a number (group) cnt, by the nature of 2 that there may be only $ $ \frac{sum}{cnt} $ $ and $ $ \frac{sum}{cnt} + 1 $ $ two values, easy to get $ $ \frac{sum}{cnt} + 1 $ $ number of occurrences of sum%cnt
Property 5: Prioritize the larger number to balance each addition and the number to be balanced
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2 * 100010 + 10;
int n;
int T;
int f[N];
struct edge
{
int val;
int cnt;
};
stack <edge> st;
signed main()
{
scanf("%lld",&T);
while(T--)
{
while(!())();
scanf("%lld",&n);
edge a;
for(int i = 1;i <= n;i++)
scanf("%lld",&f[i]);
= f[1];
= 1;
(a);
for(int i = 2;i <= n;i++)
{
= f[i];
= 1;
while( !() && / <= ().val / ().cnt)
{
edge b = ();
();
+= ;
+= ;
}
edge tt;
= / * ( - % );
= - % ;
(tt);
if( % > 0)
{
= ( / + 1) * ( % );
= % ;
(tt);
}
}
int mx = ().val / ().cnt;
int mn = -1;
while(!())
{
mn = ().val / ().cnt;
();
}
printf("%lld\n",mx - mn);
}
return 0;
}