C. Omkar and Baseball

Problem - C - Codeforces

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 nn sessions follow the identity permutation (ie. in the first game he scores 11 point, in the second game he scores 22 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][1,2,3] can yield [3,1,2][3,1,2] but it cannot yield [3,2,1][3,2,1] since the 22 is in the same position.

Given a permutation of nn 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 10181018.

An array aa is a subarray of an array bb if aa can be obtained from bb 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 tt (1≤t≤1001≤t≤100). Description of the test cases follows.

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

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

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

Output

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

Example

input

Copy

 

2

5

1 2 3 4 5

7

3 2 4 5 1 6 7

output

Copy

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 22 exchanges to sort the second permutation.

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

Perform special exchange on range (1,51,5)

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

Perform special exchange on range (1,41,4)

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

题意:选择一段区间随便排,但是每位都要动,不能呆在原位,一直到a[i]==i为止,问你最少多少步

思路:因为随便排,有三种情况

1.最多每位都不同,那么只要反一次,这样全部都可以归位了,而且不存在有人呆在原没动的。

2.如果每位都是a[i]==i,明显不用排了,剩下的那就是2位了,我怀疑我写的复杂了,直接分类讨论上面两种就行,

3.如果a[i]==i和a[i]!=i混在一起。剩下的直接输出2就行,因为要使a[i]==i的打乱顺序,不在a[i]==i的也打乱顺序,第二次翻转因为都不在a[i]==i,那么直接排成a[i]==i这种情况,所以最多两次情况。

代码应该没有我这么复杂。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int a[N],sum[N];
priority_queue<int,vector<int>,greater<int> >pq;
set<int>se;
map<int,int>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
int n;
void solve()
{
    cin>>n;
    int k=0,m=0;
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
      if(a[i]!=i&&!k)
      {
          k=1;
      }
      else if(k&&a[i]==i&&!m)
      {
          m=1;
      }
      else if(k&&m&&a[i]!=i)
      {
          flag=1;
      }
    }
    if(flag)//第三种情况
    cout<<2<<endl;
    else if(!flag&&k)
    {
        cout<<1<<endl;
    }
    else cout<<0<<endl;
}
signed main()
{
   int t=1;
   cin>>t;
   while(t--)
   {
       solve();
   }
}

今天码力还不错,哈哈哈哈哈哈哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值