I don't know how to describe it. .
Maybe you can operate on a 2-k interval (k must be a power of 2, the title gives an upper bound of k), pick the middle one as long as K or the length sum of the two ends as k, let them interval-1, and then use 2k as a cyclic node to operate on the sequence. This is an operation, asking the minimum number of times required to turn the whole sequence into the same one.
Assuming that n is a power of 2, then we start with n to see whether the current interval [1,2i][1,2i] is the same left or right. If the same interval is 2i_12i_1, it can be used as a common cyclic node for the subsequent operations, we do not need to operate on this interval. Otherwise, we pull the interval out into a loop, and the adjacent points are different. Each operation is to a segment-1 on the ring, and the change of the ring after the difference is achieved. It's a pair of positions with one + 1, one - 1. Our goal is to make the left and right intervals the same, that is, the difference values of the relative positions the same, and then recurse [1,2i_1][1,2i_1]
(Feeling unclear about qaq)
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 5100;
int n,D,m,ok,ans;
int a[maxn],b[maxn],fl[maxn];
void Solve(int L)
{
if(L==1) return;
int t=L>>1,ck=1;
for(int i=1;i<=t&&ck;i++) if(a[i]!=a[i+t]) ck=0;
if(ck) { Solve(t); return; }
if(L>2*D) { ok=0;return; }
b[1]=a[1]-a[L];
for(int i=2;i<=L;i++) b[i]=a[i]-a[i-1];
for(int i=1;i<=L;i++) fl[i]=0;
for(int i=1;i<=t;i++)
{
int c1=b[i],c2=b[i+t];
if(c1!=c2)
{
if((c1-c2)%2!=0) { ok=0;return; }
if(c1<c2)
{
int tc=(c2-c1)>>1;
fl[i]+=tc,fl[i+t]-=tc;
ans+=tc;
}
if(c1>c2)
{
int tc=(c1-c2)>>1;
fl[1]+=tc,fl[i]-=tc,fl[i+t]+=tc;
ans+=tc;
}
}
}
int now=0;
for(int i=1;i<=L;i++)
{
now+=fl[i];
a[i]+=now;
}
Solve(t);
}
int main()
{
int tcase; scanf("%d",&tcase);
for(int ti=1;ti<=tcase;ti++)
{
scanf("%d%d",&n,&D);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]=-a[i];
m=1;while((m<<1)<=n) m<<=1;
ok=1;
for(int i=m+1;i<=n&&ok;i++) if(a[i]!=a[i-m]) ok=0;
ans=0;
Solve(m);
for(int i=1;i<=n;i++) if(a[i]>0) ok=0;
printf("Case #%d: ",ti);
if(!ok) puts("CHEATERS!");
else printf("%d\n",ans);
}
return 0;
}