rt
我的算法是 N2 居然过了?
是数据水吗,还是我算法不是N2
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-')
f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-48;
ch=getchar();
}
return x*f;
}
int n=read(),a[1000005],ans=1e18;
struct node{
int id,sum;
};
queue<node> q;
signed main(){
for(int i=1; i<=n; i++)
a[i]=read();
for(int i=1; i<=n; i++){
if(a[i]<=1)
continue;
int sum=0;
bool vis=false;
while(q.empty()==false)
q.pop();
q.push(node{0,a[i]-1});
for(int j=1; j<n; j++){
int nw=(i+j-1)%n+1;
if(a[nw])
q.push(node{j,a[nw]});
if(q.empty()==true){
vis=true;
break;
}
node cur=q.front();
// q.pop();
sum+=(j-cur.id)*(j-cur.id);
if(cur.sum>1)
q.front().sum--;
else
q.pop();
// if(j==n-1)
// cout<<"asio";
}
if(vis==false)
ans=min(ans,sum);
}
cout<<ans;
return 0;
}
请勿抄袭,后果自负