C. Omkar and Baseball Codeforces Round 655 (Div. 2)

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 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 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 10181018.

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≤1001≤�≤100). Description of the test cases follows.

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

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

It is guaranteed that the sum of n� 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]

题目大意:

选一个区间进行排序,排序的区间不能有数字没有换位置,问至少几次才能让所有数字有序。

思路:

如果有两个以上位置对不上的区间,可以合并区间,但不能全合并,全合并会导致对的数字出现原位。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"

const ll N = 2e5+7;
bool vis[N];//看当前位置是否是正确的 

void solve(){
	vis[0]=1;
	ll n,x,sum=0;
	cin >> n;
	for(ll i = 1 ; i <= n ; i ++){
		cin >> x;
		x == i ? vis[i] = 1 : vis[i] = 0;
		if(!vis[i] && vis[i-1])sum++;
	}
	sum > 2 ? cout << 2 << endl : cout << sum << endl;
	return;
}

int main() {
	ll t=1;cin >> t;
	while(t--)solve();
	return 0;
}

  • 17
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值