P3137 [USACO16FEB]Circular Barn S 题解
思路
转换题意,对于第
Code
#include<bits/stdc++.h>
using namespace std;
struct node{
int walk,next,num;
}a[100005];
int n;
long long ans,ans1;
bool book[100005];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].num;
a[i].next=i%n+1; //变成环
}
int post=1;
while(ans1!=n){
if(a[post].num>1){前走
a[a[post].next].num+=a[post].num-1;
a[a[post].next].walk+=a[post].num-1;
a[post].num=1;
}
if(a[post].num==1&&book[post]==0){
book[post]=1;
ans1++;
}
post=a[post].next;
}
for(int i=1;i<=n;i++) ans+=a[i].walk*a[i].walk;
cout<<ans;
return 0;
}