Codeforces Round #655 (Div. 2) B&C題解
阿新 • • 發佈:2020-07-14
程式碼如下:
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int a[200005]; int main() { ios::sync_with_stdio(false); int t,n; cin>>t; while(t--) { bool flag=false; cin>>n; int cnt=0; memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { cin>>a[i]; } int i,j; for(i=n;i>=1;i--) { if(a[i]!=i) break; } if(i==0) { cout<<0<<endl; continue; } for(j=1;j<i;j++)//i和j不可能相等,若存在錯位元素,則至少有兩個 { if(a[j]!=j) break; } bool ok=true; for(int k=j+1;k<i;k++) { //cout<<"k=="<<k<<endl; if(a[k]==k) { cout<<2<<endl; ok=false; break; } } if(ok) cout<<1<<endl; } return 0; }