CodeForce 1372C

C. Omkar and Baseball

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up!

Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position.

Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn’t exceed 1018.

An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.

The first line of each test case contains integer n (1≤n≤2⋅105) — the length of the given permutation.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the initial permutation.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.

Example input

2
5
1 2 3 4 5
7
3 2 4 5 1 6 7

output

0
2

Note

In the first permutation, it is already sorted so no exchanges are needed.

It can be shown that you need at least 2 exchanges to sort the second permutation.

[3,2,4,5,1,6,7]
Perform special exchange on range (1,5)

[4,1,2,3,5,6,7]
Perform special exchange on range (1,4)

[1,2,3,4,5,6,7]

题目大意:

输入一个 t 表示 t 组样例,对于每组数据,先输入一个n表示数组内有n个元素,之后输入这个数组。你可以选择一个连续的区间,令区间内所有数都不在原来的位置上,输出至少几次这样的操作能使数组呈[1…n]。

解题思路:

我们首先应该考虑,操作的次数只有可能是0 1 2 次,当数组本来有序的时候,操作就是0次,当数组每个数字都不在原来的位置上的连续区间只有一个时,1次即可排好,如1 3 4 2 5 对于其他情况,先执行一次使得所有数都不在应该在的位置,再执行一次即可完全还原。设一个cnt表示这种不在原来位置连续区间的个数,如果cnt=0,则说明本来就是符合要求的,如果cnt=1,符合1次就排好的情况,其他情况直接令cnt=2,直接输出cnt即可。AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int _max = 2e5+50;
int a[_max];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof a);
        int n;
        cin>>n;
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            if(a[i]!=i&&a[i-1]==i-1)//如果符合条件则该位置是区间的开始,cnt++
              cnt++;
        }
        if(cnt>2)//其他情况最多需要2次
          cnt=2;
        cout<<cnt<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Hayasaka/p/14294218.html