B. Permutation Sort

B. Permutation Sort

You are given a permutation a consisting of n numbers 1, 2, …, n (a permutation is an array in which each element from 1 to n occurs exactly once).

You can perform the following operation: choose some subarray (contiguous subsegment) of a and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array.

For example, if a=[2,1,4,5,3] and we want to apply the operation to the subarray a[2,4] (the subarray containing all elements from the 2-nd to the 4-th), then after the operation, the array can become a=[2,5,1,4,3] or, for example, a=[2,1,5,4,3].

Your task is to calculate the minimum number of operations described above to sort the permutation a in ascending order.

Input
The first line contains a single integer t (1≤t≤2000) — the number of test cases.

The first line of the test case contains a single integer n (3≤n≤50) — the number of elements in the permutation.

The second line of the test case contains n distinct integers from 1 to n — the given permutation a.

Output
For each test case, output a single integer — the minimum number of operations described above to sort the array a in ascending order.

Example
inputCopy
3
4
1 3 2 4
3
1 2 3
5
2 1 4 5 3
outputCopy
1
0
2
Note
In the explanations, a[i,j] defines the subarray of a that starts from the i-th element and ends with the j-th element.

In the first test case of the example, you can select the subarray a[2,3] and swap the elements in it.

In the second test case of the example, the permutation is already sorted, so you don’t need to apply any operations.

In the third test case of the example, you can select the subarray a[3,5] and reorder the elements in it so a becomes [2,1,3,4,5], and then select the subarray a[1,2] and swap the elements in it, so a becomes [1,2,3,4,5].

题目大意

将数组进行划分,但不能从1~N,然后再这个划分的里面对数组进行排序,问最少划分多少次,才能使得整个数组升序排列

思路

也比较水的一个题,多试几次,就可以找到规律,虽然我w了4次,哭了

代码

#include<bits/stdc++.h>
using namespace std;
int a[100];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int max = -1;
		int min = 9999999;
		int flag= 0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(a[i]>max)
			{
				max = a[i];
			}
			if(a[i]<min)
			{
				min = a[i];
			}
		}
		for(int i=1;i<n;i++)
		{
			if(a[i]>a[i+1])
			flag = 1;
		}
		if(max==a[n]&&min==a[1])
		{
			if(flag==0)
			{
				cout<<0<<endl;
			}
			else
			{
				cout<<1<<endl;
			}
		}
		else if(max!=a[n]&&min!=a[1])
		{
			if(max==a[1]&&min==a[n])
			{
				cout<<3<<endl;
			}
			else
			{
				cout<<2<<endl;
			}
		}
		else
		{
			cout<<1<<endl;
		}
	}
	return 0;
}

.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CX__CS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值