description
analysis
-
We can use prefix and find out the number of non-adjustable schemes of the original string first.
-
For a flip, it must be to flip the interval of [i. [a] [i. [a] [i] [i. [a] [i]] [i. [i]] or [a [i]. [i]] [a [i].. [i] [a [i]. [i]].
-
It can also be considered that the point of i+a[i]2{i+a[i]}over 22i+a[i] is the turning center to flip the interval.
-
So we got all the nnn turning centers out and saved the turning length with the vector vector vector vector.
-
For each turning center, arrange the turning length from small to large.
-
Since the current length flip only affects a point from an illegal point to a legitimate point, each scheme increases.
-
Left and right endpoints [l,r][l,r][l,r], each lll becomes smaller, rrr becomes larger, the number of alternatives increases, and then add alternatives outside the area to update the answer
-
It can be said to be a clever way of thinking.
code
#pragma GCC optimize("O3") #pragma G++ optimize("O3") #include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #define MAXN 100005 #define INF 1000000007 #define ll long long #define reg register ll #define fo(i,a,b) for (reg i=a;i<=b;++i) #define fd(i,a,b) for (reg i=a;i>=b;--i) using namespace std; ll a[MAXN],sum[MAXN],pos[MAXN]; vector<ll>v[MAXN<<1]; ll n,ans; inline ll read() { ll x=0,f=1;char ch=getchar(); while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();} while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } inline ll get(ll x,ll y){return sum[y]-sum[x-1];} int main() { freopen("rotate.in","r",stdin); freopen("rotate.out","w",stdout); n=read(); fo(i,1,n)pos[a[i]=read()]=i,sum[i]=sum[i-1]+(a[i]==i); fo(i,1,n)v[i+pos[i]].push_back(abs(i-pos[i])+1); ans=sum[n]; fo(i,1,n<<1) { if (v[i].size()==0)continue; ll mid=i/2,cnt=0; sort(v[i].begin(),v[i].end()); fo(j,0,v[i].size()-1) { ll len=v[i][j],l,r; if (i%2==0)l=mid-len/2,r=mid+len/2; else l=mid-len/2+1,r=mid+len/2; ++cnt; ans=max(ans,get(1,l-1)+cnt+get(r+1,n)); } } printf("%lld\n",ans); return 0; }