1 条题解

  • 2
    @ 2022-10-7 13:26:59
    #include <iostream>
    using namespace std;
    
    /*
        说明:
        通过分(写)析(字),数组排列主要有以下几种情况
        拿一个长度9的数组举例
        1:原数组排列就是对的   0步
            1 2 3 4 5 6 7 8 9
            操作次数:0。
        2:首或尾有一个是对的   1步
            1 6 8 4 9 5 7 3 2   (首对)
            操纵后8个数字归位
                1{ 1 2 3 4 5 6 7 8 9 }
            5 2 6 1 8 7 4 3 9   (尾对)
            操纵前8个数字归位
                1{ 1 2 3 4 5 6 7 8 9}
        3:首尾在数组中间   2步
            6 4 1 2 7 3 9 8 5
                1{ 1 2 3 4 6 7 8 9 5 }
                2{ 1 2 3 4 5 6 7 8 9 }
        4:首尾颠倒     3步
            9 5 4 6 8 7 3 2 1
                1{ 2 3 4 5 6 7 8 9 1 }
                2{ 2 1 3 4 5 6 7 8 9 }
                3{ 1 2 3 4 5 6 7 8 9 }
        所有情况均可归结为这四类。
        (车万人大胜利)
    */
    
    int main()
    {
        int a[50];
        int n,t;
        
        cin>>t;
        for(int z=0;z<t;z++)
        {
            cin>>n;
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
            }
            if(a[0]==1 and a[n-1]==n) //首尾位置正确的情况
            {    
                bool flag=true;
                for(int j=0;j<n;j++)
                {
                    if(a[j]!=a[j+1]-1 and j<n-1)      //原封有动的情况
                    {
                        flag=false;
                        cout<<1<<endl;
                        break;
                    }
                }
                if(flag)    //原封不动的情况
                {
                    cout<<0<<endl;
                }
            }
            else if(a[0]==1 or a[n-1]==n)   //首尾有一个错误的情况
            {
                cout<<1<<endl;
            }
            else if(a[0]==n and a[n-1]==1)      //首尾被掉转的情况
            {
                cout<<3<<endl;
            }
            else        //首尾均位于中间的情况
            {
                cout<<2<<endl;
            }
        }
        return 0;
    }
    

    ,

    ,

    ,image

    • 1

    信息

    ID
    1270
    时间
    1000ms
    内存
    256MiB
    难度
    1
    标签
    递交数
    57
    已通过
    46
    上传者