题解:P13492 【MX-X14-T2】反转时光
liuyuantao · · 题解
题目传送门
upd on 25.8.2:更正了一些错误。
赛时猜到了结论,赛后觉得比较有趣就把它证明了,因此写一篇题解。
显然,当且仅当原排列本身有序时(即排列为
对于形如
接下来我们证明
不妨设原排列为
p=[a_1,a_2,\ldots,a_n] ,我们将其划分为[a_1,\ldots,a_i] 和[a_{i+1},\ldots,a_n] ,反转后变为[a_{i+1},\ldots,a_n,a_1,\ldots,a_i] 。继续划分为[a_{i+1},\ldots,a_j] ,[a_{j+1},\ldots,a_n,a_1,\ldots,a_i] 。反转后变为[a_{j+1},\ldots,a_n,a_1,\ldots,a_j] 。
容易发现,我们最后一定是在由(a_1,a_2,\ldots,a_n) 组成的环上找一个点断开构成链,并不能改变它们在环上的相对顺序,于是在环上若i 的后继不为i+1 ,在链上(排列中)也不可能实现。所以原排列不可能有序。
显然对于形如
最后我们证明:对于不满足
我们不妨设排列为
p=[a_1,a_2,\ldots,a_n] 。第一步,我们将1 移到首位。若a_i=1(i \ne 1) ,那么选择[a_1,\ldots,a_{i-1}] ,[] ,[a_i,\ldots,a_n] ,反转后1 即在首位。
接下来我们不妨假设1 到i 的所有数已经按顺序排在最前(i \ge 1 ,且满足对于任意1 \le j \le i ,均有a_j=j ),且a_{i+1} \ne i+1 。 那么我们选择[1,\ldots,i] ,[] ,[a_{i+1},\ldots,a_n] ,使排列变为[a_{i+1},\ldots,a_n,1,\ldots,i] 。之后若a_j=i+1 (由前得j \ne i+1 ),则选择[a_{i+1},\ldots,a_{j-1}] ,[a_j,\ldots,a_n],[1,\ldots,i] ,即可将排列变为[1,\ldots,i,i+1,\ldots] 。重复以上操作,即可完成对p 的排序。
我们令断点表示满足
代码中逻辑略有差异,但是可以 AC。
AC code
#include<bits/stdc++.h>
using namespace std;
#define PII pair<string,int>
#define LL long long
const int N=1e5+5;
const LL mod=998244353;
LL inf=0x7fffffffff;
LL T,n,m,k,l,a,b,c,ans,cnt;
LL A[N],B[N];
bool bl[N];
string s,t;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>A[i];
}
m=0;
for(int i=2;i<=n;i++){
if(A[i]!=A[i-1]+1)m++;
}
if(m==0)cout<<1<<endl;
else if(m==1&&A[n]==A[1]-1)cout<<2<<endl;
else cout<<3<<endl;
return 0;
}