Codeforces Round #655 (Div. 2) B C




代码如下:

#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;
}
posted @ 2020-07-14 14:07  肥泽~  阅读(121)  评论(0编辑  收藏  举报