CodeForces - 1525B Permutation Sort
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1525B Permutation Sort相关的知识,希望对你有一定的参考价值。
B. Permutation Sort
time limit per test: 2 seconds
memory limit per test: 256 megabytes
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
input
3
4
1 3 2 4
3
1 2 3
5
2 1 4 5 3
output
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].
问题链接:CodeForces - 1525B Permutation Sort
问题简述:(略)
问题分析:(略)
AC的C++语言程序如下:
/* CodeForces - 1525B Permutation Sort */
#include <bits/stdc++.h>
using namespace std;
const int N = 50 + 1;
int a[N];
int main()
{
int t, n;
scanf("%d", &t);
while (t--) {
int flag = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] != i) flag = 1;
}
if (a[1] != 1 && a[n] != n) flag++;
if (a[1] == n && a[n] == 1) flag++;
printf("%d\\n", flag);
}
return 0;
}
以上是关于CodeForces - 1525B Permutation Sort的主要内容,如果未能解决你的问题,请参考以下文章