exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
listlengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED
|
0d38d0ceac68980d0b8aca9ed6549c53
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class FastIO{
BufferedReader br;
StringTokenizer st;
public FastIO(){ // constructor
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
static void sort(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i = 0; i < n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
// int dir[][] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
// T.C. -> O(log N)
static int sumOfDigits(int n) {
int sum = 0;
while(n > 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
static int gcd(int a, int b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args) throws Exception{
FastIO in = new FastIO();
PrintWriter out = new PrintWriter(System.out);
int test = in.nextInt();
while(test-- > 0) {
int n = in.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
boolean isSahi = true;
for(int i = 0; i < n - 1; i++) {
if(arr[i] > arr[i + 1]) {
isSahi = false;
break;
}
}
if(isSahi) {
out.println(0);
continue;
}
if(arr[0] == n && arr[n - 1] == 1) {
out.println(3);
continue;
}
else {
if(arr[0] == 1 && arr[n - 1] != n || arr[0] != 1 && arr[n - 1] == n) {
out.println(1);
}
else if(arr[0] == 1 && arr[n - 1] == n){
out.println(1);
}
else {
out.println(2);
}
}
}
out.flush();
out.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
d154b0237e315e20c44566a1fb1feed2
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Collections;
import java.util.Arrays;
public class Main
{
public static boolean isSorted(Integer[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int l=0;l<t;l++){
int n =sc.nextInt();
Integer a[]=new Integer[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int mn = Collections.min(Arrays.asList(a));
int mx =Collections.max(Arrays.asList(a));
if(isSorted(a))
System.out.println(0);
else if(a[n-1]==mn && a[0]==mx)
System.out.println(3);
else if(a[n-1]==mx || a[0]==mn)
System.out.println(1);
else
System.out.println(2);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
d8828dc6b8f5ee73489eb566867bf479
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- != 0){
int n = s.nextInt();
int arr[] = new int[n];
int safe[] = new int[n];
for(int i = 0; i < n; i++){
arr[i] = s.nextInt();
safe[i] = arr[i];
}
Arrays.sort(arr);
if(Arrays.equals(arr,safe)){
System.out.println(0);
}
else if(arr[0] != safe[0] && arr[n - 1] != safe[n - 1]){
if(safe[0] == n && safe[n - 1] == 1){
System.out.println(3);
}
else{
System.out.println(2);
}
}
else{
System.out.println(1);
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ba8d8d58848f4fca6b418ac456d32c8a
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class B_Permutation_Sort {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main (String[] args) throws java.lang.Exception
{
int t = Integer.parseInt(br.readLine());
for(int i=0;i<t;i++){
solve();
}
}
public static void solve() throws IOException{
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] arr = new int[n];
for(int i=0;i<arr.length;i++){
arr[i] = Integer.parseInt(st.nextToken());
}
boolean sorted = true;
for(int i=0;i<n;i++){
if(arr[i]!=i+1){
sorted = false;
break;
}
}
if(sorted){
System.out.println("0");
return;
}
if(arr[0] == 1 || arr[n-1] == n){
System.out.println("1");
return;
}
if(arr[0] == n && arr[n-1] == 1){
System.out.println("3");
return;
}
System.out.println("2");
return;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
de191b7b1d6f781e1186feffca2b902b
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class Test{
static int nstep(int a[]){
int max = a[0]; int min =a[0];
for(int i=0;i<a.length;i++)
{ if(a[i]>max){max=a[i];}
else if(a[i]<min){min=a[i];}
}
if(a[0]==max && a[a.length-1]==min){return 3;}
else if(a[0]!=min && a[a.length-1]!=max){return 2;}
else{ if(a[0]==min && a[a.length-1]!=max){return 1;}
else if(a[0]!=min && a[a.length-1]==max){return 1;}
else{int l =a[0];
for(int i=1;i<a.length;i++)
{ if(l>a[i])
{return 1;}
l=a[i];
}
return 0;
}
}
}
public static void main(String args[]){
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++)
{ int t = sc.nextInt();
int a[] = new int[t];
for(int j=0;j<t;j++)
{
a[j] = sc.nextInt();
}
System.out.println(nstep(a));
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3ea18e3270804a319f35529500db400d
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-- >0)
{
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int[] arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(s[i]);
boolean flag=isSorted(arr,0,n-1);
if(flag)
System.out.println(0);
else if((arr[0]==n && arr[n-1]==1) || isRevSorted(arr))
System.out.println(3);
else if(arr[0]==1 || arr[n-1]==n)
System.out.println(1);
else
System.out.println(2);
}
}
static boolean isSorted(int [] arr,int x,int y)
{
boolean flag=true;
for(int i=x+1;i<=y;i++)
{
if(arr[i-1]>arr[i])
flag=false;
}
return flag;
}
static boolean isRevSorted(int [] arr)
{
boolean flag=true;
for(int i=1;i<arr.length;i++)
{
if(arr[i-1]<arr[i])
flag=false;
}
return flag;
}
static int search(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i]==1)
return i;
}
return 0;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
884af21798150d47e29b95f05ee554c9
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
int t=sc.nextInt();
while(t--!=0){
int n=sc.nextInt();
int[] a=new int[n];
int[] b=new int[n];
int ssc=0,pcc=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
b[i]=a[i];
}
Arrays.sort(a);
for(int i=0;i<n;i++){
if(b[i]!=a[i]){
pcc++;
if(pcc==1) ssc++;
else if(pcc==n){
ssc++;
}
}
else{
if(pcc>0) pcc++;
/*if(pcc==n){
ssc++;
}*/
}
}
if(b[n-1]==a[0] && b[0]==a[n-1]) ssc++;
System.out.println(ssc);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
222a46ff4a59fd4983ad80ae89546da0
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
// import java.math.*;
import java.io.*;
public class Main
{
public static int mod=(int)Math.pow(10,9) + 7;
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter ot=new PrintWriter(System.out);
public static int[] take_arr(int n){
int a[]=new int[n];
try {
String s[]=br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
} catch (Exception e) {
e.printStackTrace();
}
return a;
}
public static void main (String[] args) throws java.lang.Exception
{
try{
int t=Integer.parseInt(br.readLine().trim());
int cno=1;
while(t-->0)
{
String qq[]=br.readLine().trim().split(" ");
int n=Integer.parseInt(qq[0]);
// int m=Integer.parseInt(qq[1]);
int a[]=take_arr(n);
int b[]=new int[n];
for(int i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
int count=0;
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
count++;
}
// int ans=0;
// while(!check(a,n))
// {
// int ind=0;
// for(int i=0;i<n;i++)
// {
// if(a[i]!=b[i])
// {
// ind=i;
// break;
// }
// }
// count=0;
// if(ind==0)
// Arrays.sort(a,0,n-1);
// else if(ind==n-1)
// Arrays.sort(a,1,n);
// else
// {
// Arrays.sort(a,1,n);
// // bubbleSort(a, ind, n);
// }
// ans++;
// for(int i=0;i<n;i++)
// {
// if(a[i]!=b[i])
// count++;
// }
// }
// ot.println(ans);
if(count==0)
ot.println(0);
else
{
if(a[0]==b[0]||a[n-1]==b[n-1])
{
ot.println(1);
}
else if(a[0]==b[n-1]&&a[n-1]==b[0])
ot.println(3);
else
ot.println(2);
}
}
ot.close();
br.close();
}catch(Exception e){
e.printStackTrace();
return;
}
}
public static boolean check(int a[],int n){
for(int i=1;i<n;i++)
{
if(a[i]<a[i-1])
return false;
}
return true;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3713b812de04886bcb1aa71df993251e
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static int find(int n,int m,int a[][]){
int dp[][]= new int[n][m];
dp[n-1][m-1]=1;
boolean f=true;
for (int i=m-2;i>=0;i--){
if (a[n-1][i+1]==1&&f){
dp[n-1][i]=1;
}
else{
dp[n-1][i]=0;
f=false;
}
}
f=true;
for (int i=n-2;i>=0;i--){
if (a[i+1][m-1]==1&&f){
dp[i][m-1]=1;
}
else{
dp[i][m-1]=0;
f=false;
}
}
for (int i=n-2;i>=0;i--){
for (int j=m-2;j>=0;j--){
if (a[i][j]==0)dp[i][j]=0;
else dp[i][j]=dp[i+1][j]+dp[i][j+1];
}
}
return dp[0][0];
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
// int n=sc.nextInt(),m=sc.nextInt();
// int a[][]= new int[n][m];
// for (int i=0;i<n;i++){
// char ch[]=sc.next().toCharArray();
// for (int j=0;j<m;j++){
// if (ch[j]=='.'){
// a[i][j]=1;
// }
// else{
// a[i][j]=0;
// }
// }
// }
// System.out.println(find(n,m,a));
// for (int i=0;i<n;i++){
// for (int j=0;j<m;j++){
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
int t=sc.nextInt();
outer:while (t-->=1){
int n=sc.nextInt();
int a[]=sc.readArray(n);
// boolean f=false;
// int c=0;
// for (int i=0;i<n;i++){
// if ((i+1)==a[i]){
// f=true;
// c++;
// }
// }
// boolean decre=true;
// for (int i=0;i<n;i++){
// if (n-i!=a[i]){
// decre=false;
// break;
// }
// }
// if (decre){
// System.out.println(3);
// continue outer;
// }
// if (c==n){
// System.out.println(0);
// continue outer;
// }
// else if (f){
// System.out.println(1);
// }
// else{
// System.out.println(2);
// }
if (sorted(a)){
System.out.println(0);
continue outer;
}
else if (a[0]==1||a[n-1]==n){
System.out.println(1);
continue outer;
}
else if (a[0]==n&&a[n-1]==1){
System.out.println(3);
continue outer;
}
else{
System.out.println(2);
}
}
}
// out.flush();
//------------------------------------if-------------------------------------------------------------------------------------------------------------------------------------------------
//sieve
static void primeSieve(int a[]){
//mark all odd number as prime
for (int i=3;i<a.length;i+=2){
a[i]=1;
}
for (long i=3;i<a.length;i+=2){
//if the number is marked then it is prime
if (a[(int) i]==1){
//mark all muliple of the number as not prime
for (long j=i*i;j<a.length;j+=i){
a[(int)j]=0;
}
}
}
a[2]=1;
a[1]=a[0]=0;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static String sortString(String s) {
char temp[] = s.toCharArray();
Arrays.sort(temp);
return new String(temp);
}
static class Pair implements Comparable<Pair> {
String a;
int b;
public Pair(String a, int b) {
this.a = a;
this.b = b;
}
// to sort first part
// public int compareTo(Pair other) {
// if (this.a == other.a) return other.b > this.b ? -1 : 1;
// else if (this.a > other.a) return 1;
// else return -1;
// }
public int compareTo(Pair other) {
// if (this.b == other.b) return 0;
if (this.b > other.b) return 1;
else return -1;
}
//sort on the basis of first part only
// public int compareTo(Pair other) {
// if (this.a == other.a) return 0;
// else if (this.a > other.a) return 1;
// else return -1;
// }
}
static int[] frequency(String s){
int fre[]= new int[26];
for (int i=0;i<s.length();i++){
fre[s.charAt(i)-'a']++;
}
return fre;
}
static long LOWERBOUND(long a[],long k){
int s=0,e=a.length-1;
long ans=-1;
while (s<=e){
int mid=(s+e)/2;
if (a[mid]<=k){
ans=mid;
s=mid+1;
}
else {
e=mid-1;
}
}
return ans;
}
static long mod =(long)(1e9+7);
static long mod(long x) {
return ((x % mod + mod) % mod);
}
static long div(long a,long b){
return ((a/b)%mod+mod)%mod;
}
static long add(long x, long y) {
return mod(mod(x) + mod(y));
}
static long mul(long x, long y) {
return mod(mod(x) * mod(y));
}
//Fast Power(logN)
static int fastPower(long a,long n){
int ans=1;
while (n>0){
long lastBit=n&1;
if (lastBit==1){
ans=(int)mul(ans,a);
}
a=(int)mul(a,a);
n>>=1;
}
return ans;
}
static int[] find(int n, int start, int diff) {
int a[] = new int[n];
a[0] = start;
for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff;
return a;
}
static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}
static void printArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
static boolean sorted(int a[]) {
int n = a.length;
boolean flag = true;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) flag = false;
}
if (flag) return true;
else return false;
}
public static int findlog(long n) {
if (n == 0)
return 0;
if (n == 1)
return 0;
if (n == 2)
return 1;
double num = Math.log(n);
double den = Math.log(2);
if (den == 0)
return 0;
return (int) (num / den);
}
public static long gcd(long a, long b) {
if (b % a == 0)
return a;
return gcd(b % a, a);
}
public static int gcdInt(int a, int b) {
if (b % a == 0)
return a;
return gcdInt(b % a, a);
}
static void sortReverse(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
// Collections.sort.(l);
Collections.sort(l, Collections.reverseOrder());
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(long n) {
long[] a = new long[(int) n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
72ae59f67efb9a3bbe0399a0017f6c04
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class B{
static long x=1000000000+7;
public static FastReader sc=new FastReader();
public static PrintWriter out = new PrintWriter(System.out);
public static void taskSolver() {
int n = sc.nextInt();
int arr[] = new int[n];
int index = -1;
boolean b=false;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (arr[i] == 1) index = i;
if(i>1 && arr[i]<arr[i-1])b=true;
}
int count = 0;
if(index==arr.length-1){
if(arr[0]==n)out.println(3);
else out.println(2);
}
else {
if(index==0 && b==false)
out.println(0);
else if(index==0 && b)out.println(1);
else{
if(arr[arr.length-1]==n)out.println(1);
else out.println(2);
}
}
}
public static void main(String args[]) throws java.lang.Exception{
int t=sc.nextInt();
while(t-->0)
taskSolver();
out.close();
}
static class Pair{
int x,y;
Pair(int x,int y){
this.x=x;this.y=y;
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
7db089f53f532f45797e35a6037b7597
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.Comparator;
import java.util.stream.IntStream;
import java.util.ArrayDeque;
public class Check {
private static FastScanner fs=new FastScanner();
private static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = fs.nextInt();
outer: while(t-->0)
{
int n = fs.nextInt();
int arr[] = fs.readArray(n);
boolean isOk = true;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=(i+1))
{
isOk = false;
break;
}
}
if(isOk)
{
System.out.println(0);
continue outer;
}
if(arr[0]==n && arr[n-1]==1)
{
System.out.println(3);
continue outer;
}
if(arr[0]==1 || arr[n-1]==n)
{
System.out.println(1);
}
else System.out.println(2);
}
}
static final Random random=new Random();
static void ruffleSort(int [] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n); int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
int [] sort(int [] arr)
{
List<Integer> list = new ArrayList<>();
for(int i : arr) list.add(i);
Collections.sort(list);
int res[] = new int[arr.length];
for(int i=0;i<arr.length;i++) res[i] = list.get(i);
return res;
}
void debug(int a)
{
System.out.println("This is var "+a);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3fde776b3b06dd4d7dde5b6326221eb6
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pranay2516
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BPermutationSort solver = new BPermutationSort();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class BPermutationSort {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.readArray(n);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (a[i] != i + 1) {
ans++;
break;
}
}
if (ans == 0) {
out.println(0);
} else {
if (a[0] == 1 || a[n - 1] == n) {
out.println(1);
} else {
if (a[0] == n && a[n - 1] == 1) {
out.println(3);
} else {
out.println(2);
}
}
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] readArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = nextInt();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
8f488f27752999a4adb0202446257d2e
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main {
static PrintWriter pw;
static Scanner sc;
static StringBuilder ans;
static long mod = 1000000000+7;
static void pn(final Object arg) {
pw.print(arg);
pw.flush();
}
/*-------------- for input in an value ---------------------*/
static int ni() { return sc.nextInt(); }
static long nl() { return sc.nextLong(); }
static double nd() { return sc.nextDouble(); }
static String ns() { return sc.next(); }
static void ap(int arg) { ans.append(arg); }
static void ap(long arg) { ans.append(arg); }
static void ap(String arg) { ans.append(arg); }
static void ap(StringBuilder arg) { ans.append(arg); }
/*-------------- for input in an array ---------------------*/
static void inputIntegerArray(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void inputLongArray(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void inputStringArray(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void inputDoubleArray(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*-------------- File vs Input ---------------------*/
static void runFile() throws Exception {
sc = new Scanner(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
}
static void runIo() throws Exception {
pw =new PrintWriter(System.out);
sc = new Scanner(System.in);
}
static void swap(int a, int b) {
int t = a;
a = b;
b = t;
}
static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);}
static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean sv[] = new boolean[1000002];
static void seive() {
//true -> not prime
// false->prime
sv[0] = sv[1] = true;
sv[2] = false;
for(int i = 0; i< sv.length; i++) {
if( !sv[i] && (long)i*(long)i < sv.length ) {
for ( int j = i*i; j<sv.length ; j += i ) {
sv[j] = true;
}
}
}
}
static long binpow( long a, long b) {
long res = 1;
while (b > 0) {
if ( (b & 1) > 0){
res = (res * a)%mod;
}
a = (a * a)%mod;
b >>= 1;
}
return res;
}
static long factorial(long n) {
long res = 1, i;
for (i = 2; i <= n; i++){
res = ((res%mod) * (i%mod))%mod;
}
return res;
}
static class Pair {
int idx;
int v;
Pair(int idx, int v){
this.idx = idx;
this.v = v;
}
}
public static void main(String[] args) throws Exception {
// runFile();
runIo();
int t;
t = 1;
t = sc.nextInt();
ans = new StringBuilder();
while( t-- > 0 ) {
solve();
}
pn(ans+"");
}
public static void solve() {
int n = ni();
int ar[] = new int[n];
inputIntegerArray(ar);
int count = 0;
if ( ( n == ar[0] ) && ( 1 == ar[n-1] ) ) {
count = 3;
ap(count+"\n");
return;
}
for(int i = 0; i<n; i++){
if( (i+1) != ar[i] ){
count = 1;
break;
}
}
if ( ( 1 != ar[0] ) && ( n != ar[n-1] ) ) {
count = 2;
}
ap(count+"\n");
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
b5703c7bca51245d8cff2ca13a9ccee4
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class B
{
public static void main(String[] args) throws IOException
{
//StringBuffer sb=new StringBuffer("");
int ttt=1;Scanner sc=new Scanner(System.in);
ttt =sc.nextInt();
outer: while(ttt-->0)
{StringBuilder ab=new StringBuilder();
int n=sc.nextInt();
int[] a=new int[n];int[] b=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
b[i]=a[i];
}
Arrays.sort(a);boolean f=true;int ans=0;int min=-1;int max=-1;
for(int i=0;i<n;i++)
{
if(a[i]==b[0])
{
min=i;
}
if(a[i]==b[n-1])
max=i;
if(a[i]!=b[i])
{
f=false;
}
}
if(f==true)
ans=0;
else if(min==0||max==n-1)
ans=1;
else if(min==n-1&&max==0)
ans=3;
else
ans=2;
System.out.println(ans);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
bf874e5d3655877d5cdec5986987deee
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// 4 3 2 1
// 2 3 4 1
// 2 3 1 4
// 3 2 1
public class CDFB {
private static void solve(int[] a) {
int n = a.length;
int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = a[i];
}
Arrays.sort(c);
boolean f = false;
for (int i = 0; i < n; i++) {
if (c[i] != a[i]) {
f = true;
break;
}
}
if (!f) {
System.out.println(0);
return;
}
if (c[n - 1] == a[0] && c[0] == a[n - 1]) {
System.out.println(3);
return;
}
if (c[0] == a[0] && c[n - 1] == a[n - 1]) {
System.out.println(1);
} else if (c[0] == a[0] || c[n - 1] == a[n - 1]) {
System.out.println(1);
} else {
System.out.println(2);
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int[] a = sc.readArrayInt(n);
solve(a);
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[] readArrayInt(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
6d32145c2145faa75eab78e41f0f8921
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
public class First {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int T = fs.nextInt();
for (int tt = 0; tt < T; tt++) {
int n=fs.nextInt();
int[] arr=take1(n, fs);
boolean alreadySorted=true;
for(int i=1;i<n;++i)
{
if(arr[i-1]>arr[i])
{
alreadySorted=false;
break;
}
}
if(alreadySorted)
{
System.out.println(0);
continue;
}
int smallest=n+1, largest=0;
int smallestIdx=-1, largestIdx=n+2;
for(int i=0;i<n;++i)
{
if(arr[i]<smallest)
{
smallest=arr[i];
smallestIdx=i;
}
if(arr[i]>largest)
{
largest=arr[i];
largestIdx=i;
}
}
boolean fullDecr=true;
for(int i=1;i<n;++i)
{
if(arr[i-1]<arr[i])
{
fullDecr=false;
break;
}
}
if(fullDecr)
{
System.out.println(3);
continue;
}
if(smallestIdx==n-1 && largestIdx==0)
{
System.out.println(3);
continue;
}
if(smallestIdx==0 && largestIdx==n-1)
{
System.out.println(1);
}
else if(smallestIdx==0 || largestIdx==n-1)
{
System.out.println(1);
}
else
System.out.println(2);
}
}
static int solve()
{
return 0;
}
static int MOD=(int)(1e9+7);
static void debug1(int[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debug2(int[][] arr)
{
for(int i=0;i<arr.length;++i)
for(int j=0;j<arr[0].length;++j)
System.out.println(arr[i][j]+" ");
System.out.println();
}
static int[] take1(int n, FastScanner fs)
{
int[] arr=new int[n];
for(int i=0;i<n;++i)
arr[i]=fs.nextInt();
return arr;
}
static int[][] take2(int m, int n, FastScanner fs)
{
int[][] arr=new int[m][n];
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
arr[i][j]=fs.nextInt();
return arr;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
33e891d750f928dc586b0fd5026b0d39
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
public class permsort
{
static int find(int a[])
{
int n = a.length;
boolean flag = true;
for (int i=0; i<n; i++)
{
if (a[i]!=i+1)
{
flag = false;
break;
}
}
if (flag)
return 0;
if (a[0]==1 || a[n-1]==n)
return 1;
if (a[0]==n && a[n-1]==1)
return 3;
return 2;
}
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String inp[];
int t = Integer.parseInt(in.readLine());
for (int i=0; i<t; i++)
{
int n = Integer.parseInt(in.readLine());
int a[] = new int[n];
inp = in.readLine().split(" ");
for (int j=0; j<n; j++)
a[j] = Integer.parseInt(inp[j]);
System.out.println(find(a));
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
83f8c388317e4348486c211fb1df70f8
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class gfg {
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException{
Reader.init(System.in);
int t = Reader.nextInt();
while(t-->0){
solve();
}
out.close();
}
// static int n =0,m=0;
static long mod =1000000007;
static int MOD = 1000000007;
static int[] dx = {0,0,1,-1};
static int[] dy = {1,-1,0,0};
//static int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
static ArrayList<Integer>[] graph;
static int n=0,m=0;
static int first=0,dist=0,max=0;
static long[] arr;
//static HashSet<Integer> set = new HashSet<>();
static HashMap<Integer,Integer> map = new HashMap<>();
static Boolean[][] dp;
static void solve() throws IOException{
int n = Reader.nextInt();
int[] arr = new int[n];
int min = 100000;
int max = -1;
for(int i=0;i<n;i++){
arr[i] = Reader.nextInt();
min = Math.min(min,arr[i]);
max = Math.max(max,arr[i]);
}
int[] arr2 = new int[n];
for (int i=0;i<n;i++){
arr2[i]=arr[i];
}
Arrays.sort(arr2);
boolean sorted = true;
for(int i=0;i<n;i++){
if(arr[i]!=arr2[i])
sorted=false;
}
if(sorted){
System.out.println(0);
return;
}
else{
if(arr[0]==min || arr[n-1]==max){
System.out.println(1);
return;
}
else if(arr[0]==max & arr[n-1]==min){
//check if it can be divided into two subarrays which can be sorted
System.out.println(3);
return;
}
else{
System.out.println(2);
return;
}
}
}
// ggratest common divisor println
static int gcd(int a , int b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
// least common multiple
static int lcm(int a, int b){
return (a*b)/gcd(a,b);
}
// a^b
static long fastpow(long a, long b){
long res = 1;a=a%mod;
while(b>0){
if(b%2==1)
res = (res*a)%mod;
a = (a*a)%mod;
b = b>>1;
}
return res;
}
// SEGMENT TREE
// static void update(int cur,int val, int ind, int l, int h){
// if(l==h){
// seg[cur] = val;
// return;
// }
// int mid = (l+h)/2;
// if(ind<=mid){
// update(2*cur+1,val,ind,l,mid);
// }
// else{
// update(2*cur+2,val,ind,mid+1,h);
// }
// seg[cur] = seg[2*cur+1] + seg[2*cur+2];
// }
// static int query(int cur, int l, int h , int reql, int reqh){
// if(reql>reqh || l>h)
// return 0;
// if(l==reql && h==reqh)
// return seg[cur];
// int mid = (l+h)/2;
// return query(2*cur+1,l,mid,reql,Math.min(reqh,mid)) + query(2*cur+2,mid+1,h,Math.max(mid+1,reql),reqh);
//}
// priority queue with comparator
// PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
// public int compare(Interval a, Interval b){
// int diff1 = a.r - a.l;
// int diff2 = b.r - b.l;
// if(diff1 == diff2){
// return a.l - b.l;
// }
// return diff2 - diff1;
// }
// });
}
class trienode{
int v;
trienode[] child ;
boolean leaf;
int cnt;
int p=0;
trienode(int vv){
child = new trienode[2];
leaf=false;
cnt=0;
v=vv;
}
trienode(){
child = new trienode[2];
leaf=false;
cnt=0;
v=0;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
class newcomparator implements Comparator<Integer>{
//@Override
public int compare(Integer a, Integer b){
return a<=b?1:-1;
}
}
class node {
int f;
int s;// cost to rreach\
node(int ff,int ss){
f=ff;
s=ss;
}
}
class mergesort{
static void sort(int l, int h, int[] arr){
if(l<h){
int mid = (l+h)/2;
sort(l,mid,arr);
sort(mid+1,h,arr);
merge(l,mid,h,arr);
}
}
static void merge(int l, int m , int h , int [] arr){
int[] left = new int[m-l+1];
int[] right = new int[h-m];
for(int i= 0 ;i< m-l+1;i++){
left[i] = arr[l+i];
}
for(int i=0;i<h-m;i++){
right[i] = arr[m+1+i];
}
//now left and right arrays are assumed to be sorted and we have tp merge them together
// int the original aaray.
int i=l;
int lindex = 0;
int rindex = 0;
while(lindex<m-l+1 && rindex<h-m){
if(left[lindex]<=right[rindex]){
arr[i]=left[lindex];
lindex++;
i++;
}
else{
arr[i]=right[rindex];
rindex++;
i++;
}
}
while(lindex<m-l+1){
arr[i]=left[lindex];
lindex++;
i++;
}
while(rindex<h-m){
arr[i]=right[rindex];
rindex++;
i++;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ab0fddc08eda6512e09a7b09d9dcaed1
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class AiseHi {
static Scanner sc = new Scanner(System.in);
static int mod = (int)(1e9+7);
public static void main (String[] args) {
int t = 1;
t = sc.nextInt();
z : while(t-->0) {
int n = sc.nextInt();
int a[] = new int[n];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
boolean is = true;
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
min = Math.min(min, a[i]);
max = Math.max(max, a[i]);
if(i>=1) is &= (a[i]>=a[i-1]);
}
if(is) {
System.out.println(0);
}
else if(a[0] == min || a[n-1] == max) {
System.out.println("1");
}
else if(a[0]==max && a[n-1]==min) {
System.out.println("3");
}
else
System.out.println("2");
}
}
// static int ceil(int a,int b) {
// return a/b + (a%b==0?0:1);
// }
static boolean prime[] = new boolean[2000009];
// static int fac[] = new int[2000009];
static void sieve() {
prime[0] = true;
prime[1] = true;
int max = 1000000;
for(int i=2;i*i<=max;i++) {
if(!prime[i]) {
for(int j=i*i;j<=max;j+=i) {
prime[j] = true;
// fac[j] = i;
}
}
}
}
static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); }
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
6de3a56b51ffb38f73f33601e008ca0b
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
//import javafx.util.*;
public final class B
{
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<ArrayList<Integer>> g;
static long mod=(long)(1e9+7);
static int D1[],D2[],par[];
static boolean set[];
static int value[];
static long INF=Long.MAX_VALUE;
static long dp[];
static int N,M;
static int A[][],B[][];
static int s=1;
public static void main(String args[])throws IOException
{
int T=i();
outer:while(T-->0)
{
int N=i();
int A[]=input(N);
if(isSorted(A))
{
System.out.println(0);
}
else
{
if(A[0]==N && A[N-1]==1)System.out.println(3);
else if(A[0]!=1 && A[N-1]!=N)System.out.println(2);
else System.out.println(1);
}
}
}
static boolean isSorted(int A[])
{
int N=A.length;
for(int i=0; i<N-1; i++)
{
if(A[i]>A[i+1])return false;
}
return true;
}
static int f1(int x,ArrayList<Integer> A)
{
int l=-1,r=A.size();
while(r-l>1)
{
int m=(l+r)/2;
int a=A.get(m);
if(a<x)l=m;
else r=m;
}
return l;
}
static int ask(int t,int i,int j,int x)
{
System.out.println("? "+t+" "+i+" "+j+" "+x);
return i();
}
static int ask(int a)
{
System.out.println("? 1 "+a);
return i();
}
static int f(int st,int end,int d)
{
if(st>end)return 0;
int a=0,b=0,c=0;
if(d>1)a=f(st+d-1,end,d-1);
b=f(st+d,end,d);
c=f(st+d+1,end,d+1);
return value[st]+max(a,b,c);
}
static int max(int a,int b,int c)
{
return Math.max(a, Math.max(c, b));
}
static int value(char X[],char Y[])
{
int c=0;
for(int i=0; i<7; i++)
{
if(Y[i]=='1' && X[i]=='0')return -1;
if(X[i]=='1' && Y[i]=='0')c++;
}
return c;
}
static boolean isValid(int i,int j)
{
if(i<0 || i>=N)return false;
if(j<0 || j>=M)return false;
return true;
}
static long fact(long N)
{
long num=1L;
while(N>=1)
{
num=((num%mod)*(N%mod))%mod;
N--;
}
return num;
}
static boolean reverse(long A[],int l,int r)
{
while(l<r)
{
long t=A[l];
A[l]=A[r];
A[r]=t;
l++;
r--;
}
if(isSorted(A))return true;
else return false;
}
static boolean isSorted(long A[])
{
for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false;
return true;
}
static boolean isPalindrome(char X[],int l,int r)
{
while(l<r)
{
if(X[l]!=X[r])return false;
l++; r--;
}
return true;
}
static long min(long a,long b,long c)
{
return Math.min(a, Math.min(c, b));
}
static void print(int a)
{
System.out.println("! "+a);
}
static int ask(int a,int b)
{
System.out.println("? "+a+" "+b);
return i();
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
par[a]+=par[b]; //transfers the size
par[b]=a; //changes the parent
}
}
static void swap(char A[],int a,int b)
{
char ch=A[a];
A[a]=A[b];
A[b]=ch;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void setGraph(int N)
{
g=new ArrayList<ArrayList<Integer>>();
for(int i=0; i<=N; i++)
{
g.add(new ArrayList<Integer>());
}
}
static long pow(long a,long b)
{
long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
//Debugging Functions Starts
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
//Debugging Functions END
//----------------------
//IO FUNCTIONS STARTS
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
//IO FUNCTIONS END
}
class node implements Comparable<node>
{
int l,r,index;
node(int l,int r,int index)
{
this.l=l;
this.r=r;
this.index=index;
}
public int compareTo(node X)
{
return X.r-this.r;
}
}
class node1 implements Comparable<node1>
{
int l,r,index;
node1(int l,int r,int index)
{
this.l=l;
this.r=r;
this.index=index;
}
public int compareTo(node1 X)
{
if(this.l==X.l)
return X.r-this.r;
return this.l-X.l;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
//gey
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
581b876cf9642ff7daf58b4eacaaf715
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
public class cf1525Bhw4 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int l = 0; l<t; l++){
int n = scan.nextInt();
boolean iszero = true;
int[] a = new int [n];
a[0]= scan.nextInt();
for (int i = 1; i<n; i++){
a[i]= scan.nextInt();
if (a[i] < a[i-1]){
iszero = false;
}
}
if (iszero == false){
int first = a[0];
int last = a[n-1];
Arrays.sort(a);
if (first == a[n-1] && last == a[0]) {
System.out.println("3");
}else if(first != a[0] && last != a[n-1]){
System.out.println("2");
}else{
System.out.println("1");
}
}else{
System.out.println("0");
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
e0bac48d791350107a1574802ba7c4e8
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
public class cf1525B {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
// int[] arr = new int[n];
// int counter = 0;
//
// for (int i = 0; i < n; i++) {
// arr[i] = scan.nextInt();
// }
// Arrays.sort(arr);
// if (n <= 2) {
// System.out.println("0");
// return;
// }
// for (int i = 1; i < n - 1; i++) {
// if (arr[0] < arr[i] && arr[n - 1] > arr[i]) {
// counter = counter + 1;
// }
// }
// System.out.println(counter);
for (int i = 0; i < n; i++) {
int x = scan.nextInt();
int arr[] = new int[x];
int min = 1;
int max = x;
boolean sorted = true;
for (int l = 0; l < x; l++) {
arr[l] = scan.nextInt();
if (arr[l] != l + 1) {
sorted = false;
}
}
if (sorted) {
System.out.println("0");
} else if (arr[0] == max && arr[x - 1] == min) {
System.out.println("3");
} else if ((arr[0] == min || arr[x - 1] == max)) {
System.out.println("1");
} else {
System.out.println("2");
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
22e9127ee16c02319b181f9a704b89a3
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.StringTokenizer;
public class Permutation_Sort
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0)
{
break;
}
else
{
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
{
c = read();
}
boolean neg = (c == '-');
if (neg)
{
c = read();
}
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
{
return -ret;
}
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
{
c = read();
}
boolean neg = (c == '-');
if (neg)
{
c = read();
}
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
{
return -ret;
}
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
{
c = read();
}
boolean neg = (c == '-');
if (neg)
{
c = read();
}
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
{
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
{
buffer[0] = -1;
}
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
{
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
{
return;
}
din.close();
}
}
public static int gcd (int a, int b)
{
if (b == 0)
{
return a;
}
return gcd (b, a % b);
}
public static void main(String Args[]) throws java.lang.Exception
{
try
{
Reader obj = new Reader();
int t = obj.nextInt();
while (t > 0)
{
t--;
int n = obj.nextInt();
int arr[] = new int[n];
int i, c = 0;
for (i=0;i<n;i++)
{
arr[i] = obj.nextInt();
if (arr[i] == i + 1)
{
c++;
}
}
if (c == n)
{
System.out.println ("0");
}
else if (arr[0] == 1 || arr[n - 1] == n)
{
System.out.println ("1");
}
else if (arr[n - 1] == 1 && arr[0] == n)
{
System.out.println ("3");
}
else
{
System.out.println ("2");
}
}
}catch(Exception e){
return;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ef69c528ec8b4174a06f0287c7555213
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class cf1525B {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int counter = 0;
int[] intArr = new int[n];
int[] intArrSort = new int[n];
for (int j = 0; j < n; j++) {
intArr[j] = scan.nextInt();
intArrSort[j] = intArr[j];
}
Arrays.sort(intArrSort);
for (int a = 0; a < n; a++) {
if (intArr[a] == intArrSort[a]) {
counter++;
}
}
if (counter == n){
System.out.println(0);
}else if (intArr[0] == intArrSort[0] || intArr[n-1] == intArrSort[n-1]) {
System.out.println(1);
} else if (intArr[0] == intArrSort[n-1] && intArr[n-1] == intArrSort[0]) {
System.out.println(3);
} else {
System.out.println(2);
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
58dd0d59cd701402cb56aa5352588268
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter o = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
int l[] = a.clone();
Arrays.sort(a);
boolean g = false;
for (int i = 0; i < n; i++) {
if (l[i] != a[i]) {
g = true;
break;
}
}
if (!g)
o.println(0);
else {
if (l[0] == a[n - 1] && a[0] == l[n-1]) {
o.println(3);
} else if (a[0] != l[0] && a[n - 1] != l[n - 1]) {
o.println(2);
} else {
o.println(1);
}
}
}
o.close();
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n), temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void shuffleSort(long[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n);
long temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
int[] readArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
e7bb9a7a941dd0dd64c409ac1bfcf443
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n+1];
int sorted = 0;
boolean edge = false;
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
if (a[i] == i) {
sorted++;
if (i == 1 || i == n) edge = true;
}
}
boolean invEdge = a[1] == n && a[n] == 1;
if (sorted == n) out.println(0);
else if (edge) out.println(1);
else if (invEdge) out.println(3);
else out.println(2);
}
out.close();
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n), temp = a[r];
a[r] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-') {
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else {
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i] = nextInt();
return a;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
2db077851d3fdd1c3503702d48e18d1e
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int t1 = 0; t1 < t; t1++) {
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
pw.println(solve(arr, n));
}
pw.close();
}
static int solve(int[] arr, int n) {
int[] copy = Arrays.copyOf(arr, n);
Arrays.sort(copy);
boolean good = true;
for (int i = 0; i < n; i++) {
if (arr[i] != copy[i]) {
good = false;
break;
}
}
if (good) return 0;
if (arr[0] == n && arr[n - 1] == 1) return 3;
if (arr[0] == 1 || arr[n - 1] == n) return 1;
return 2;
}
static void debug(Object... Obj) {
System.err.println(Arrays.deepToString(Obj));
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
88a55ff91385e4f7f20ec31e544fa313
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class helloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCase = 1;
testCase = sc.nextInt();
while(testCase-- > 0) {
int n = sc.nextInt();
int [] array = new int[n];
for(int i = 0 ; i < n ; i++){
array[i] = sc.nextInt();
}
boolean flg = true;
for(int i = 0 ; i < n - 1; i++){
if(array[i] > array[i + 1]){
flg = false ;
break;
}
}
if(array[0] == 1 && array[n - 1] == n && flg) System.out.println("0");
else if((array[0] == 1 && !flg) || (array[n - 1] == n && !flg) || (array[0] == 1 && array[n - 1] == n && !flg)) System.out.println("1");
else if(array[0] == n && array[n - 1] == 1) System.out.println("3");
else System.out.println("2");
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
bf24feb3c780c2c83478247f2351c326
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
//import javafx.util.*;
public class Solution
{
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static int INF = Integer.MAX_VALUE;
static int NINF = Integer.MIN_VALUE;
public static void main (String[] args) throws java.lang.Exception
{
//check if you have to take product or the constraints are big
int t = i();
while(t-- > 0){
int n = i();
int[] arr = input(n);
int[] temp = new int[n];
for(int i = 0;i < n;i++){
temp[i] = arr[i];
}
sort(temp);
boolean sorted = true;
int min = INF;
int max = NINF;
for(int i = 0;i < n;i++){
min = Math.min(min,arr[i]);
max = Math.max(max,arr[i]);
if(temp[i] != arr[i]){
sorted = false;
}
}
if(sorted){
out.println(0);
continue;
}
if(arr[0] == min || arr[n-1] == max){
out.println(1);
continue;
}
if(arr[0] == max && arr[n-1] == min){
out.println(3);
continue;
}
out.println(2);
}
out.close();
}
public static void sort(int[] arr){
ArrayList<Integer> ls = new ArrayList<>();
for(int x : arr){
ls.add(x);
}
Collections.sort(ls);
for(int i = 0;i < arr.length;i++){
arr[i] = ls.get(i);
}
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
}
class Pair implements Comparable<Pair>{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair obj)
{
// we sort objects on the basis of Student Id
return (this.x - obj.x);
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
39d702add7ef83cb0ea6f096c82fc3bb
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int d, int i) {
this.d = d;
this.i = i;
}
public int compareTo(Pair o) {
return this.d - o.d;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Reader sc = new Reader();
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
boolean flag=true;
for(int i=1;i<=n;i++)
if(arr[i-1]!=i)
flag=false;
if(flag)
{
sb.append("0");
}
else
{
if(arr[0]==1||arr[n-1]==n)
sb.append("1");
else
if(arr[0]==n&&arr[n-1]==1)
{
sb.append("3");
}
else
{
sb.append("2");
}
}
sb.append("\n");
}
System.out.println(sb);
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static boolean isPos(int idx, long[] arr, long[] diff) {
if (idx == 0) {
for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) {
diff[idx] = i;
arr[0] -= i;
arr[1] -= i;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[0] += i;
arr[1] += i;
}
} else if (idx == 1) {
if (arr[2] - arr[1] >= 0) {
long k = arr[1];
diff[idx] = k;
arr[1] = 0;
arr[2] -= k;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[1] = k;
arr[2] += k;
} else
return false;
} else {
if (arr[2] == arr[0] && arr[1] == 0) {
diff[2] = arr[2];
return true;
} else {
return false;
}
}
return false;
}
public static boolean isPal(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array.get(mid)) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
6b2c4e6330495c62e960e805a9cfd5a2
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class PermutationSort{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
int arr[] = sc.readIntArray(n);
boolean flag = true;
for(int i = 0;i<n-1;i++){
if(arr[i]>arr[i+1]){
flag = false;
break;
}
}
if(flag){
out.println(0);
}else{
if(arr[0]==1 || arr[n-1]==n){
out.println(1);
}else if(arr[0]==n && arr[n-1]==1){
out.println(3);
}else{
out.println(2);
}
}
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1 || n==2) return false;
if(n==3) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
static class Pair implements Comparable<Pair>{
int val;
int freq;
Pair(int v,int f){
val = v;
freq = f;
}
public int compareTo(Pair p){
return this.freq - p.freq;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-- >0){
// solve();
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
458450d800623113e704cd2f4069f673
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) {
// TODO Auto-generated method stub
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while (t-->0)
{
int n=fs.nextInt();
int ans=2;
int [] a=new int [n];
int f=0;
for (int i=0;i<n;i++)
{
a[i]=fs.nextInt();
if (a[i]!=i+1) f=1;
}
if (f==0) ans=0;
else if (a[0]==1 || a[n-1]==n)
ans=1;
else if (a[0]==n && a[n-1]==1)
ans=3;
System.out.println(ans);
}
}
static class FastScanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
}catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt () {
return Integer.parseInt(next());
}
long nextLong () {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
2b384e71114ad7d91effa3460095fc40
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int ar[] = new int[n+1];
boolean flag=false;
for(int i=1;i<=n;i++) {
ar[i]=sc.nextInt();
if(ar[i]!=i) flag=true;
}
if(flag==false) System.out.println(0);
else {
if(ar[1]==1 || ar[n]==n)
System.out.println(1);
else if(ar[1]==n && ar[n]==1)
System.out.println(3);
else
System.out.println(2);
}
}
sc.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
7f198e5061064dda54c156c7e29a95cf
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Permutation_Sort{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int tests = scanner.nextInt();
int size = 0;
boolean check = false;
int[] arr;
for(int i=0; i<tests; i++){
check = false;
size = scanner.nextInt();
arr = new int[size];
for(int j=0; j<size; j++){
arr[j] = scanner.nextInt();
if(arr[j] != j+1) check = true;
}
if(!check){
System.out.println(0);
}else if(arr[0] == 1 || arr[size-1] == size){
System.out.println(1);
}else if(arr[0] == size && arr[size-1] == 1){
System.out.println(3);
}else{
System.out.println(2);
}
}
}
static void sort(int[] arrInput){
List<Integer> list=new ArrayList<Integer>();
for(int i: arrInput){
list.add(i);
}
Collections.sort(list);
for(int i=0; i<arrInput.length; i++){
arrInput[i] = list.get(i);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ce6beb09fe4e70e79344560960722fb4
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import static java.lang.Math.*;
import java.io.*;
public class S {
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test > 0){
test--;
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int a[] = new int[n];
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(st.nextToken());
}
solve(n, a);
//System.out.println(s + " " + tar);
//StringTokenizer st = new StringTokenizer(br.readLine());
//int n = Integer.parseInt(st.nextToken());
//int l = Integer.parseInt(st.nextToken());
//st = new StringTokenizer(br.readLine());
//int a[] = new int[n];
//for(int i = 0; i < n; i++){
// a[i] = Integer.parseInt(st.nextToken());
//}
}
}
final static int MOD = 1000000007;
public static void solve(int n, int a[]){
boolean sorted = true;
for(int i = 0; i < n; i++){
if(a[i] != i+1){
sorted = false;
}
}
if(sorted){
System.out.println(0);
return;
}
if(a[0] == 1 || a[n-1] == n){
System.out.println(1);
return;
}
if(a[0] == n && a[n-1] == 1){
System.out.println(3);
return;
}
System.out.println(2);
}
public static int gcd(int a, int b){
if(b == 0)return a;
return gcd(b, a%b);
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
c1d5c8092fac9c4aef3c8e6b8e737e4b
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class PermutationSort {
private static final Scanner SCANNER=new Scanner(System.in);
public static void main(String[] args){
int testcase=SCANNER.nextInt();
while (testcase-->0){
int length=SCANNER.nextInt();
int[] array=new int[length+1];
for (int i=0;i<length;i++){
array[i]=SCANNER.nextInt();
array[i]--;
}
boolean check=true;
for (int i=0;i<length;i++){
if (array[i]!=i) {
check=false;
}
}
if (check==true) System.out.println(0);
else if (array[0]==0 || array[length-1]==length-1)System.out.println(1);
else if (array[0]==length-1 && array[length-1]==0)System.out.println(3);
else System.out.println(2);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3d47d96c85b16c46ea290938d69e9d97
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class Permutation_sort
{
int noO(int arr[],int n,int max,int min)
{
int flag=0;
for(int i=1;i<n;i++)
{
if(arr[i]<arr[i-1])
{
flag=1;
break;
}
}
if(flag==0)
return 0;
else
{
if(arr[0]==min||arr[n-1]==max)
return 1;
if(arr[0]==max&&arr[n-1]==min)
return 3;
else
return 2;
}
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int ii=0;ii<t;ii++)
{
int n=in.nextInt();
int arr[]=new int[n];
int max=-1;
int min=100;
for(int i=0;i<n;i++)
{
arr[i]=in.nextInt();
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
System.out.println(new Permutation_sort().noO(arr,n,max,min));
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
197082c2728a5530f471e6d6d374f2ec
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class snippe{
static void print(String s){
System.out.println(s);
}
static void print(int s){
System.out.println(s);
}
static int[] rep(int[] arr,int n){
Scanner sc=new Scanner(System.in);
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
return arr;
}
static void solve(){
Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static long spd(long n){
for(int i=2;i*i<=n;i++){
if(n%i==0){
return i;
}
}
return n;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
while(k-->0){
int n=sc.nextInt();
int[]arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int max=arr[0];
int min=arr[0];
boolean flag=true;
for(int i=1;i<n;i++){
if(arr[i]<arr[i-1]){
flag=false;
}
max=Math.max(max,arr[i]);
min=Math.min(min,arr[i]);
}
if(flag){
System.out.println(0);
continue;
}
if(max==arr[n-1]||min==arr[0]){
System.out.println(1);
}
else if(arr[0]==max&&arr[n-1]==min){
System.out.println(3);
}
else{
System.out.println(2);
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
c9077fb0650f7ab5793cb2db41adbe58
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class p1525B {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-->0){
int n = in.nextInt();
boolean sorted = true;
boolean minDisplaced = false;
boolean maxDisplaced = false;
boolean minIsMax = false;
boolean maxIsMin = false;
for (int i = 1; i <= n; i++){
int a = in.nextInt();
if (i==1){
if (a!=1)
minDisplaced = true;
if (a==n)
minIsMax = true;
} else if (i==n){
if (a!=n)
maxDisplaced = true;
if (a==1)
maxIsMin = true;
} else if (i!=a)
sorted = false;
}
if (maxIsMin && minIsMax)
System.out.println(3);
else if (maxIsMin || minIsMax || minDisplaced && maxDisplaced)
System.out.println(2);
else if (minDisplaced || maxDisplaced || !sorted)
System.out.println(1);
else
System.out.println(0);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3ae97a79e2bd0e041361e47f6b3982d7
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B extends Driver {
public static void main(String[] args) {
new B().run();
}
@Override
public void solveOne() {
int n = in.nextInt();
long[] a = in.nextLongArray(n);
if (LongArrayAlgos.isSorted(a)) {
System.out.println(0);
return;
}
if (a[0] == n && a[n - 1] == 1) {
System.out.println(3);
return;
}
if (a[0] == 1 || a[n - 1] == n) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
class LongArrayAlgos {
public void reverse(long[] a) {
int lo = 0, hi = a.length - 1;
while (lo < hi) {
long tmp = a[lo];
a[lo] = a[hi];
a[hi] = tmp;
lo++;
hi--;
}
}
public static boolean isSorted(long[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
return false;
}
}
return true;
}
public static void sort(long[] a) {
sort(a, 0, a.length);
}
public static void sort(long[] a, int lx, int rx) {
if (tryInsertionSort(a, lx, rx)) {
return;
}
long[] ws = new long[rx];
sort2(a, lx, rx, ws);
}
private static void sort2(long[] a, int lx, int rx, long[] ws) {
if (tryInsertionSort(a, lx, rx)) {
return;
}
int m = (lx + rx) / 2, i1 = lx, i2 = m, i3 = lx;
sort2(a, lx, m, ws);
sort2(a, m, rx, ws);
System.arraycopy(a, lx, ws, lx, rx - lx);
while (i1 < m || i2 < rx) {
if (i2 >= rx || (i1 < m && ws[i1] <= ws[i2])) {
a[i3++] = ws[i1++];
} else {
a[i3++] = ws[i2++];
}
}
}
private static boolean tryInsertionSort(long[] a, int lx, int rx) {
if (rx - lx > 80) {
return false;
}
for (int i = lx + 1; i < rx; i++) {
long x = a[i];
int j = i - 1;
while (j >= lx && a[j] > x) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = x;
}
return true;
}
}
class Driver {
public final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public final FastScanner in = new FastScanner();
public final void run() {
try {
solve();
} finally {
close();
}
}
public void solve() {
int tt = in.nextInt();
while (tt-- > 0) {
solveOne();
}
}
public void solveOne() {
throw null;
}
protected void close() {
out.close();
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public long nextLong() {
return Long.parseLong(next());
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
b5d1112f480de26a414c0e3f38225cec
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class P1525B {
private static FastScanner in;
public static void main(String[] args) {
in = new FastScanner();
int n = in.nextInt();
for (int i = 0; i < n; i++) {
solve();
}
}
private static void solve() {
int n = in.nextInt();
long[] a = in.readLongArray(n);
if (ArrayUtils.isSorted(a)) {
System.out.println(0);
return;
}
if (a[0] == n && a[n - 1] == 1) {
System.out.println(3);
return;
}
if (a[0] == 1 || a[n - 1] == n) {
System.out.println(1);
} else {
System.out.println(2);
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public long nextLong() {
return Long.parseLong(next());
}
}
static class ArrayUtils {
public static boolean isSorted(long[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
return false;
}
}
return true;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
4374a11dd7bde3d62e0db79dc4a080a1
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class _1525B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
boolean check = true;
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
check = false;
break;
}
}
if (check)
System.out.println(0);
else {
if (arr[0] == 1 || arr[n - 1] == n)
System.out.println(1);
else if (arr[0] == n && arr[n - 1] == 1)
System.out.println(3);
else
System.out.println(2);
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
952a2dc26225b800027fd954d84ed613
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
// package codeforces;
import java.util.*;
public class permutation_Sort {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
int[]arr=new int[n];
boolean b=false;
for(int i=0;i<n;i++) {
arr[i]=scn.nextInt();
if(arr[i]!=i+1) {
b=true;
}
}
if(!b) {
System.out.println(0);
continue;
}
// for(int i=0;i<n;i++) {
//
// }
if(arr[0]==n && arr[n-1]==1) {
System.out.println(3);
}else if(arr[0]!=1 && arr[n-1]!=n) {
System.out.println(2);
}else {
System.out.println(1);
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
48100cc5c7d3ef61b1aa6f8a8801de04
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Test {
static boolean issorted(int[] arr) {
for(int i=0;i<arr.length-1;i++) {
if(arr[i] > arr[i+1]) {
return false;
}
}
return true;
}
static int permute(int[] arr, int count, int n) {
if(issorted(arr)) {
return count;
} else {
if(arr[0] == 1 && arr[n-1] == n) {
count = 1;
} else if(arr[0] == n && arr[n-1] == 1) {
count = 3;
} else if((arr[0] == 1 && arr[n-1] != n) || (arr[0] != 1 && arr[n-1] == n)) {
count = 1;
} else {
count = 2;
}
}
return count;
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = Integer.parseInt(s.nextLine());
while(t > 0) {
int n = s.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++) {
arr[i] = s.nextInt();
}
System.out.println(permute(arr, 0, n));
t--;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
75a17989e76f1396159e4564becaa7d2
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.lang.*;
public class Solution {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer("");
String next() {
if (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
public static void main(String[] args) {
new Solution().solve();
}
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
void solve() {
int t = 1;
t = nextInt();
for (int tt = 0; tt < t; tt++) {
int n = nextInt();
int arr[] = new int[n];
boolean flag = true;
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
if (arr[i] != i + 1)
flag = false;
}
if (flag) {
out.println(0);
} else {
if (arr[n - 1] == 1 && arr[0]==n) {
out.println(3);
}
else if (arr[0] == 1 || arr[n - 1] == n) {
out.println(1);
} else {
out.println(2);
}
}
}
out.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3e4e94c0687f6eeee4dadc99bef32407
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
public class PermutationSort implements Closeable {
private final InputReader in;
private final PrintWriter out;
public PermutationSort() {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
public PermutationSort(String input, String output) throws FileNotFoundException {
in = new InputReader(new FileInputStream(input));
out = new PrintWriter(new FileOutputStream(output));
}
public void solve() {
int t = in.ni();
while (t-- > 0) {
int n = in.ni();
int[] x = new int[n];
int prev = -1;
boolean sorted = true;
for (int i = 0; i < n; i++) {
x[i] = in.ni();
sorted &= x[i] > prev;
prev = x[i];
}
if (sorted) {
out.println(0);
} else if (x[0] == 1 || x[n - 1] == n) {
out.println(1) ;
} else if (x[0] == n && x[n - 1] == 1) {
out.println(3);
} else {
out.println(2);
}
}
}
@Override
public void close() throws IOException {
in.close();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public void close() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws IOException {
try (PermutationSort instance = new PermutationSort()) {
instance.solve();
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
55a2b1bc8367604534d6688325d4ea6e
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
private static int[] rai(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
return arr;
}
private static int[][] rai(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextInt();
}
}
return arr;
}
private static long[] ral(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
return arr;
}
private static long[][] ral(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextLong();
}
}
return arr;
}
private static int ri() {
return s.nextInt();
}
private static long rl() {
return s.nextLong();
}
private static String rs() {
return s.next();
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static long gcd(long a,long b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static boolean isPrime(int n) {
//check if n is a multiple of 2
if(n==1)
{
return false;
}
if(n==2)
{
return true;
}
if (n % 2 == 0) return false;
//if not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
static boolean[] sieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean prime[] = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static void main(String[] args) {
StringBuilder ans = new StringBuilder();
int t = ri();
// int t=1;
while (t-- > 0)
{
int n=ri();
int[] arr=rai(n);
boolean flag = true;
for(int i=1;i<n;i++)
{
if(arr[i]<arr[i-1])
{
flag = false;
break;
}
}
if(flag)
{
ans.append("0\n");
continue;
}
if(arr[0]==1 || arr[n-1]==n)
{
ans.append("1\n");
}
else
{
if(arr[n-1]==1 && arr[0]==n)
{
ans.append("3\n");
}
else {
ans.append("2\n");
}
}
}
out.print(ans.toString());
out.flush();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
732698706b232dfcdfedb6328ad150f8
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class CF_1525b{
public static final void main(String[] args){
Scanner s= new Scanner(System.in);
int t= Integer.parseInt(s.nextLine());
for(int ti=0; ti<t; ti++){
int k= Integer.parseInt(s.nextLine());
String[] arr= s.nextLine().split(" ");
int[] perm= new int[k];
boolean isSorted= true;
for(int i=0; i<k; i++){
perm[i]= Integer.valueOf(arr[i]);
if(isSorted && perm[i]!=i+1) isSorted= false;
}
boolean first1= perm[0]==1, lastK= perm[k-1]==k;
boolean firstK= perm[0]==k, last1= perm[k-1]==1;
// 0, 1 or 2 sorts on subarrays to get to sorted array
int res= isSorted ? 0 : first1 || lastK ? 1 : firstK && last1 ? 3 : 2;
System.out.println(res);
}
s.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
35e63b8291ae0809a4a5bdac5c57fc4b
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static long gdc(long a, long b) {
if (b > a) {
long t = a;
a = b;
b = t;
}
while (a % b != 0) {
long t = a % b;
a = b;
b = t;
}
return b;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1024 * 1024 * 2);
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
int[] v = readArrayLine(br.readLine(), n);
boolean sorted = true;
for (int i = 1; i < n ; i ++) {
if (v[i] < v[i - 1]) {
sorted = false;
break;
}
}
int count = 0;
if (!sorted) {
if (v[0] == 1 || v[n - 1] == n) {
count = 1;
} else if (v[0] == n && v[n-1] == 1){
count =3 ;
} else {
count = 2;
}
}
sb.append(count).append('\n');
}
System.out.println(sb.toString());
}
private static long solve() {
return 0;
}
public static int[] readArrayLine(String line, int n) {
return readArrayLine(line, n, null, 0);
}
private static int[] readArrayLine(String line, int n, int array[], int pos) {
int[] ret = array == null ? new int[n] : array;
int start = 0;
int end = line.indexOf(' ', start);
for (int i = pos; i < pos + n; i++) {
if (end > 0)
ret[i] = Integer.parseInt(line.substring(start, end));
else
ret[i] = Integer.parseInt(line.substring(start));
start = end + 1;
end = line.indexOf(' ', start);
}
return ret;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
1df5dbd47c76232bef4c2b23922cd8a1
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
/**
*
* @author eslam
*/
public class PermutationSort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
int a[] = new int[n];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int l = 0;
for (int x = 0; x < n; x++) {
a[x] = input.nextInt();
max = Math.max(a[x], max);
min = Math.min(min, a[x]);
}
if(a[n-1]!=max){
l++;
}
if(a[0]!=min){
l++;
}
if(a[0]==max&&a[n-1]==min){
l++;
}
if(l==0){
for (int j = 0; j < n-1; j++) {
if(a[j]>a[j+1]){
l++;
break;
}
}
}
System.out.println(l);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
22c15886e03cee2be61c2f97b7918368
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class codeforce{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
if(test>=1 && test<=2000){
int t=0;
while(t<test){
int n = sc.nextInt();
int[] arr = new int[n+1];
int count=0;
boolean b=true;
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
arr[i]--;
}
for(int i=0;i<n;i++){
if(arr[i]!=i){
b = false;
break;
}
}
if(b==true){
count=0;
}
else if(arr[0]==0 || arr[n-1]==n-1){
count=1;
}
else if(arr[0]==n-1 && arr[n-1]==0){
count=3;
}
else{
count=2;
}
System.out.println(count);
t++;
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
58139d2ded35308d7531c490669e2e76
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
//package codeforces;
import java.util.Scanner;
public class PermutationSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int arr[]=new int[n];
int count=0;
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
for(int i=1;i<=n;i++) {
if(i!=arr[i-1]) {
count=-1;
}
}
if(count==0) {
}
else if(arr[0]==n && arr[n-1]==1) {
count=3;
}
else if(arr[0]==1 || arr[n-1]==n) {
count=1;
}
else if(arr[0]!=1 && arr[n-1]!=n) {
count=2;
}
System.out.println(count);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
b099d1ff466fbf536ed148b8ad8c04b2
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.io.File;
import java.io.*;
import java.util.*;
public class Main {
// static final File ip = new File("input.txt");
// static final File op = new File("output.txt");
// static {
// try {
// System.setOut(new PrintStream(op));
// System.setIn(new FileInputStream(ip));
// } catch (Exception e) {
// }
// }
public static void main(String[] args) {
FastReader sc = new FastReader();
int test = sc.nextInt();
while(test-- != 0)
{
int n = sc.nextInt();
int[] a = new int[n];
boolean check = false;
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
if(i>=1 && a[i] < a[i-1] && check==false)
{
check = true;
}
}
if(check == false)
{
System.out.println("0");
}
else if((a[0] == 1 || a[n-1]==n))
{
System.out.println("1");
}
else if(a[0]==n && a[n-1]==1)
{
System.out.println("3");
}
else
{
System.out.println("2");
}
}
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static int countSetBits(long number) {
int count = 0;
while (number > 0) {
++count;
number &= number - 1;
}
return count;
}
private static <T> void swap(T[] array, int i, int j) {
if (i != j) {
T tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
private static long getSum(int[] array) {
long sum = 0;
for (int value : array) {
sum += value;
}
return sum;
}
private static boolean isPrime(int x) {
if (x < 2)
return false;
for (int d = 2; d * d <= x; ++d) {
if (x % d == 0)
return false;
}
return true;
}
private static int[] getPrimes(int n) {
boolean[] used = new boolean[n + 1];
used[0] = used[1] = true;
int size = 0;
for (int i = 2; i <= n; ++i) {
if (!used[i]) {
++size;
for (int j = 2 * i; j <= n; j += i) {
used[j] = true;
}
}
}
int[] primes = new int[size];
for (int i = 0, cur = 0; i <= n; ++i) {
if (!used[i]) {
primes[cur++] = i;
}
}
return primes;
}
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
private static long gcd(long a, long b) {
return (a == 0 ? b : gcd(b % a, a));
}
static void shuffleArray(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleList(ArrayList<Long> arr) {
int n = arr.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr.get(i);
int randomPos = i + rnd.nextInt(n - i);
arr.set(i, arr.get(randomPos));
arr.set(randomPos, tmp);
}
}
static void shuffleArrayL(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() {
return false;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
2345dc0fbf46a2df0d7a03c3b44ae7f7
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.io.File;
import java.io.*;
import java.util.*;
public class Main {
// static final File ip = new File("input.txt");
// static final File op = new File("output.txt");
// static {
// try {
// System.setOut(new PrintStream(op));
// System.setIn(new FileInputStream(ip));
// } catch (Exception e) {
// }
// }
public static void main(String[] args) {
FastReader sc = new FastReader();
int test = sc.nextInt();
while(test-- != 0)
{
int n = sc.nextInt();
int[] a = new int[n];
boolean check = false;
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
if(i>=1 && a[i] < a[i-1] && check==false)
{
check = true;
}
}
if(check == false)
{
System.out.println("0");
}
else if((a[0] == 1 || a[n-1]==n) && check == true)
{
System.out.println("1");
}
else if(a[0]==n && a[n-1]==1 && check == true)
{
System.out.println("3");
}
else
{
System.out.println("2");
}
}
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static int countSetBits(long number) {
int count = 0;
while (number > 0) {
++count;
number &= number - 1;
}
return count;
}
private static <T> void swap(T[] array, int i, int j) {
if (i != j) {
T tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
private static long getSum(int[] array) {
long sum = 0;
for (int value : array) {
sum += value;
}
return sum;
}
private static boolean isPrime(int x) {
if (x < 2)
return false;
for (int d = 2; d * d <= x; ++d) {
if (x % d == 0)
return false;
}
return true;
}
private static int[] getPrimes(int n) {
boolean[] used = new boolean[n + 1];
used[0] = used[1] = true;
int size = 0;
for (int i = 2; i <= n; ++i) {
if (!used[i]) {
++size;
for (int j = 2 * i; j <= n; j += i) {
used[j] = true;
}
}
}
int[] primes = new int[size];
for (int i = 0, cur = 0; i <= n; ++i) {
if (!used[i]) {
primes[cur++] = i;
}
}
return primes;
}
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
private static long gcd(long a, long b) {
return (a == 0 ? b : gcd(b % a, a));
}
static void shuffleArray(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleList(ArrayList<Long> arr) {
int n = arr.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr.get(i);
int randomPos = i + rnd.nextInt(n - i);
arr.set(i, arr.get(randomPos));
arr.set(randomPos, tmp);
}
}
static void shuffleArrayL(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() {
return false;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
bf6384e0a46d94b8833d32996cc03089
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution {
public final static FastScanner fs=new FastScanner();
public static void main(String[] args) {
int t = fs.nextInt();
while (t-- > 0) solve();
}
private static void solve(){
int n = fs.nextInt();
int[] arr = new int[n];
int ans = 2;
for(int i=0; i<n; i++) {
arr[i] = fs.nextInt();
}
if(isSort(arr)) ans = 0;
else if(arr[0]==1 || arr[n-1]==n) ans = 1;
else if(arr[0]==n && arr[n-1]==1) ans = 3;
System.out.println(ans);
}
private static boolean isSort(int[] arr) {
for(int i=1; i<= arr.length; i++) {
if(arr[i-1] != i) return false;
}
return true;
}
private static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException ignored) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
fa5a542aec89151306bf04643a8c8066
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution {
private static final int MOD = (int) (1e9 + 7);
public final static FastScanner fs=new FastScanner();
public static void main(String[] args) {
int t = fs.nextInt();
while (t-- > 0) solve();
}
private static void solve(){
int n = fs.nextInt();
int ans=2;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = fs.nextInt();
}
if(isSorted(arr)) ans=0;
else if(arr[0] == 1 || arr[n-1]== n) ans=1;
else if(arr[0]==n && arr[n-1]==1) ans = 3;
System.out.println(ans);
}
private static boolean isSorted(int[] arr){
for(int i=1; i<=arr.length; i++){
if(arr[i-1] != i) return false;
}
return true;
}
private static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException ignored) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
eab21c932d1897e7a7d45f6875b62c67
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class main{
public static void main(String[]args){
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int j=0;j<t;j++){
int n=s.nextInt();
int step=0;
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
if(a[i]!=i+1){
step++;
}
}
if(step==0){
System.out.println("0");
continue;
}
if((a[0]==1)||(a[n-1]==n)){
System.out.println("1");
}
else if((a[0]==n)&&(a[n-1]==1)){
System.out.println("3");
}
else{
System.out.println("2");
}
}
} }
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
7b1ba8d3aa0e8b64a4f9e1d902abf989
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.PrintStream;
import java.util.Scanner;
public class Task {
static Scanner input = new Scanner(System.in);
static PrintStream output = new PrintStream(System.out);
static int[] a = new int[50];
public static void main(String[] args) {
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
boolean sorted = true;
boolean has1 = false;
boolean hasN = false;
int j = 0;
for (; j < n; j++) {
a[j] = input.nextInt();
if (a[j] == 1) {
has1 = true;
} else if (a[j] == n) {
hasN = true;
}
if (j > 0) {
if (a[j] < a[j - 1]) {
sorted = false;
}
if (has1 && hasN) {
input.nextLine();
break;
}
}
}
boolean allIterated = j == n - 1;
int result;
if (sorted && allIterated) {
result = 0;
} else {
result = 1;
if (allIterated) {
if (a[0] != 1 && a[n - 1] != n) {
result++;
}
if (a[0] == n && a[n - 1] == 1) {
result++;
}
} else {
if (a[0] != 1) {
result++;
}
}
}
output.println(result);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
d17b765adead87444cb2942a321fc411
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.PrintStream;
import java.util.Scanner;
public class Task {
static Scanner input = new Scanner(System.in);
static PrintStream output = new PrintStream(System.out);
static int[] a = new int[50];
public static void main(String[] args) {
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
boolean sorted = true;
for (int j = 0; j < n; j++) {
a[j] = input.nextInt();
if (j > 0 && a[j] < a[j - 1]) {
sorted = false;
}
}
int result;
if (sorted) {
result = 0;
} else {
result = 1;
if (a[0] != 1 && a[n - 1] != n) {
result++;
}
if (a[0] == n && a[n - 1] == 1) {
result++;
}
}
output.println(result);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
484a02d9d2177c8aa87cf8bdcf14ed1d
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.PrintStream;
import java.util.Scanner;
public class Task {
static Scanner input = new Scanner(System.in);
static PrintStream output = new PrintStream(System.out);
static int[] a = new int[50];
public static void main(String[] args) {
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
boolean sorted = true;
for (int j = 0; j < n; j++) {
int aj = a[j] = input.nextInt();
if (aj > max) {
max = aj;
}
if (aj < min) {
min = aj;
}
if (j > 0 && a[j - 1] > aj) {
sorted = false;
}
}
int result;
if (sorted) {
result = 0;
} else {
result = 1;
int firstValue = a[0];
int lastValue = a[n - 1];
if (firstValue != min && lastValue != max) {
result++;
}
if (firstValue == max && lastValue == min) {
result++;
}
}
output.println(result);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
a3b36a7473aa1686a19142b209ee80e9
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.PrintStream;
import java.util.Scanner;
public class Task {
static Scanner input = new Scanner(System.in);
static PrintStream output = new PrintStream(System.out);
static int[] a = new int[50];
public static void main(String[] args) {
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
boolean sorted = true;
for (int j = 0; j < n; j++) {
a[j] = input.nextInt();
if (a[j] > max) {
max = a[j];
}
if (a[j] < min) {
min = a[j];
}
if (j > 0 && a[j - 1] > a[j]) {
sorted = false;
}
}
int result;
if (sorted) {
result = 0;
} else {
result = 1;
if (a[0] != min && a[n - 1] != max) {
result++;
}
if (a[0] == max && a[n - 1] == min) {
result++;
}
}
output.println(result);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
a86220e9ba787813b1408ff87c746492
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class Main
{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader kb = new FastReader();
int t=kb.nextInt();
while(t>0){
int n=kb.nextInt();
boolean isSorted=true;
boolean isPlaced=false;
int []arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=kb.nextInt();
}
for(int i=0;i<n;i++)
{
if(arr[i]!=i+1)
{
isSorted=false;
break;
}
}
if(arr[0]==1||arr[n-1]==n)
isPlaced=true;
if(isSorted)
System.out.println("0");
else if(arr[0]==n&&arr[n-1]==1)
{
System.out.println("3");
}
else if(arr[0]==1||arr[n-1]==n)
{
System.out.println("1");
}
else{
if(isPlaced)
System.out.println("1");
else
System.out.println("2");
}
t--;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
338ea26125fcccd27003ddb463ea1a6e
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class test
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int k =0;k<t;k++)
{ int n = sc.nextInt();
int a [] = new int[n];
int ans =0;
for(int i =0;i<n;i++)
a[i] = sc.nextInt();
int c =0;
for(int i =0;i<n;i++)
{if(a[i]!=i+1)
c++;
}
int d =0;
for(int i =1;i<n;i++)
{if(a[i]<a[i-1])
d++;
}
if(a[0]==n&&a[n-1]==1)
System.out.println(3);
else if(c==0)
System.out.println(0);
else if((a[0]==1||a[n-1]==n)&&(c>0))
System.out.println(1);
else
System.out.println(2);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
1524a10cfddd429739cf3f7cf252eaab
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int counter = 0;
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
if (t >= 1 && t <= 2000)
{
for (int i = 0; i < t; i++)
{
int n = scanner.nextInt();
if (n >= 3 && n <= 50)
{
int[] array = new int[n];
for (int x = 0; x < n; x++)
{
array[x] = scanner.nextInt();
if (array[x] != (x + 1))
{
counter++;
}
}
if (counter == 0) {
System.out.println(0);
}
else if ((array[0] == 1 || array[n-1] == n)) {
System.out.println(1);
}
else if (array[0] == n && array[n-1] == 1)
{
System.out.println(3);
}
else
{
System.out.println(2);
}
counter = 0;
}
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
1a209b96e3a3965deac922e8eec3907a
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class Solution{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int ts=sc.nextInt();
while(ts-- > 0){
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int count=0;
for(int i=0;i<n;i++){
if(arr[i]!=i+1){
count++;
break;
}
}
if(count==0){
System.out.println("0");
}
else if(arr[0]==1 || arr[n-1]==n){
System.out.println("1");
}
//else if(arr[0]!=1 && arr[n-1]!=n){
// System.out.println("2");
//}
else if(arr[0]==n &&arr[n-1]==1) {
System.out.println("3");
}
else
System.out.println(2) ;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ba467b5c79d88d049e05aafaddc2d349
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
public class B {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
private static int[] rai(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
return arr;
}
private static int[][] rai(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextInt();
}
}
return arr;
}
private static long[] ral(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
return arr;
}
private static long[][] ral(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextLong();
}
}
return arr;
}
private static int ri() {
return s.nextInt();
}
private static long rl() {
return s.nextLong();
}
private static String rs() {
return s.next();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc=new FastReader();
int t,n,i=0,j=0,k=0,c=0,fg=0,p=0,q=0,a=0,b=0,tm,x=0,y=0,z=0,sum=0,sum1=0,sum2=0,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
String str;
BigInteger f=BigInteger.valueOf(1);
t=sc.nextInt();
while(t-->0)
{
n=sc.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
if(arr[i]==i+1)
sum++;
}
if(sum==n)
System.out.println(0);
else if(arr[0]==1||arr[n-1]==n)
System.out.println(1);
else if(arr[n-1]==1&&arr[0]==n)
System.out.println(3);
else
System.out.println(2);
sum=0;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
99871e5664024972fb8d500e5c9e72fd
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
//package codeforces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
public class contest {
static class Reader { // Code for Reader class taken from DSA Course reference
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Reader.init(System.in);
int t=Reader.nextInt();
// System.out.println(t);
for (int i = 0; i < t; i++) {
int n=Reader.nextInt();
int arr[]=new int[n];
//System.out.println(n);
for (int j = 0; j < n; j++) {
arr[j]=Reader.nextInt();
// System.out.println(arr[j]);
}
int s=0;
int count[][]=new int[2][2];
int c=0;
for (int j = 0; j < n; j++) {
if(arr[j]==j+1) {
s++;
}
else {
if(c<2) {
count[c][0]=arr[j];
count[c][1]=j+1;
}
c++;
}
}
if(s==n) {
System.out.println(0);
continue;
}
else if(arr[0]==1 || arr[n-1]==n) {
System.out.println(1);
continue;
}
else if(arr[0]==n && arr[n-1]==1) {
System.out.println(3);
continue;
}
else if(c==2) {
if(count[0][0]==count[1][0]+1 && count[0][1]+1==count[1][1]) {
System.out.println(1);
continue;
}
}
else {
System.out.println(2);
continue;
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
f6a96913945f2b1dce04eacf3875fc12
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void sort(int []arr) {
ArrayList<Integer> copy=new ArrayList<>();
for(int val:arr) {
copy.add(val);
}
Collections.sort(copy);
for(int i=0;i<arr.length;i++) {
arr[i]=copy.get(i);
}
}
public static int gcd(int a,int b) {
if(b==0) {
return a;
}
return gcd(b,a%b);
}
public static int lcm(int a,int b) {
return (a*b)/gcd(a,b);
}
public static boolean alreadySorted(long []arr) {
for(int i=0;i<arr.length;i++) {
if(arr[i]!=i+1) {
return false;
}
}
return true;
}
public static boolean alreadySortedDec(long []arr) {
for(int i=0;i<arr.length;i++) {
if(arr[i]!=arr.length-i) {
return false;
}
}
return true;
}
public static void main(String[] args) {
FastReader scn=new FastReader();
int t=scn.nextInt();
while(t-->0) {
int n=scn.nextInt();
long []arr=new long[n];
for(int i=0;i<arr.length;i++) {
arr[i]=scn.nextLong();
}
if(alreadySorted(arr)) {
System.out.println("0");
}else {
if(arr[0]==n&&arr[n-1]==1) {
System.out.println("3");
}else if(arr[0]==1||arr[n-1]==n) {
System.out.println("1");
}else {
System.out.println("2");
}
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
10d51724020b5a4bed31ae8cb768b7fe
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
/*Author Adityaraj*/
import java.io.*;
import java.util.*;
import java.math.*;
public class Template {
// static int count = 0 ;
public static void main(String[] args) throws IOException {
long start = System.nanoTime();
// initialize the mod variabel of very big prime number
final int mod = 1000000007;
// Initialize the reader
FastScanner scan = new FastScanner();
// Initialize the writer
FastOutput out = new FastOutput();
// Intitialize the Map of <integer,integer> --------------------------Map
// Map<Integer,Integer> map = new HashMap<>();
// Intitialize the iterator for the map-------------Map-Iterator
// Iterator<Map.Entry<Integer, Integer>> itr = map.entrySet().iterator();
// Intitialize the PriorityQueue of <Integer> with comparator or use the
// Collections.reverseOrder() to sort the element in the desecnding order
// -----------------------------------------------------------------------------PriorityQueue
// Queue<Integer> pq = new PriorityQueue<Integer>();
// ----------------------------------------------------------------------------Simple
// Queue
// Queue<Pair> pq = new LinkedList<Pair>();
// --------------------------------------------------------------PriorityQueue<Pair>
// With Lambda Comparator and Pair
// Queue<Pair> pq = new PriorityQueue<>(
// (Pair p1, Pair p2) -> {
// if (p1.w < p2.w)
// return -1;
// else if (p1.w > p2.w)
// return 1;
// else
// return 0;
// });
// Queue<Pair> pq = new LinkedList<>();
// ----------------------------------------------------------------Set
// Element in the set are not in the sorted order
// Set<Integer> set = new HashSet<Integer>();
// ---------------------------------------------------------------SortedSet
// use the Collection.reverseOrder() to sort the set in the decreasign order
// SortedSet<Integer> set = new TreeSet<Integer>();
// For the graph adjacent list ---------------------------------Adjacency--List
// List<List<Pair>> adjlist = new ArrayList<>(10);
/************************************************************************************************************************************/
// writer your code here
int t =scan.readInt();
while(t-->0){
int n = scan.readInt();
int[] arr =scan.readIntArray(n);
int index =-1;
int eindex = -1;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
if(sorted(arr)){
out.writeInt(0);
continue;
}
if(arr[0]==1||arr[n-1]==n){
out.writeInt(1);
continue;
}
if(arr[0]==n&&arr[n-1]==1){
out.writeInt(3);
continue;
}
out.writeInt(2);
// if(index==n-1){
// Arrays.sort(arr,1,n);
// Arrays.sort(arr,0,2);
// if(sorted(arr)){
// out.writeInt(2);
// continue;
// }else{
// out.writeInt(3);
// continue;
// }
// }
// Arrays.sort(arr,0,index+1);
// if(sorted(arr)) {
// out.writeInt(1);
// continue;
// }else{
// out.writeInt(2);
// }
// if(!sorted(arr)){
// out.writeInt(3);
// }
}
// your code end here
/*************************************************************************************************************************************/
// compute the time elapsed
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
long end = System.nanoTime();
System.out.print((end - start) / 1000000 + " ms");
} catch (Exception exception) {
}
}
}
/**************************************************************************************************************************************/
// do not touch it
/**************************************************************************************************************************************/
// Write here the function which do you want to insert into the code during the
// sumbition
public static boolean sorted(int[] arr){
boolean flag = true;
int n =arr.length;
for (int i = 0; i < n-1; i++) {
if(arr[i]>arr[i+1]){
flag = false;
}
}
return flag;
}
// this function will the gcd of two numbers
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// this will return the pow of a^b
public static long binexp(long a, long b) {
long res = 1;
while (b != 0) {
if (b % 2 != 0)
res *= a;
a *= a;
b /= 2;
}
return res;
}
public static long binexpomod(long a, long b, long mod) {
long res = 1;
a %= mod;
while (b != 0) {
if (b % 2 != 0)
res = (res % mod) * (a % mod);
a = (a % mod) * (a % mod);
b /= 2;
}
return res;
}
// this will return true if a is prime and false if not
public static boolean primeornot(long a) {
for (int i = 2; i * i <= a; i++) {
if (a % i == 0) {
// System.out.println(i);
return false;
}
}
return true;
}
// this function will check that a given string is palindrome or not
public static boolean palindrome(String string) {
StringBuffer buffer = new StringBuffer(string);
buffer = buffer.reverse();
if (string.equals(buffer.toString())) {
return true;
}
return false;
}
// this function count the number of digit in a number
public static int countdigit(long a) {
int count = 0;
while (a != 0) {
a = a / 10;
count++;
}
return count;
}
// this funciton will count the number of bit in a binary representation of a
// number
public static int countbit(Long n) {
int count = 0;
while (n > 0) {
count++;
n &= n - 1;
}
return count;
}
// this method will return the sum of its digit
public static int sumofdigits(int digit) {
int sum = 0;
while (digit > 0) {
sum += digit % 10;
digit /= 10;
}
return sum;
}
// this method check that a number is a perfect square or not
public static boolean perfectsquare(long n) {
if (n >= 0) {
int sr = (int) Math.sqrt(n);
return sr * sr == n;
}
return false;
}
/*************************************************************************************************************************/
/*************************************************************************************************************************/
// pair class of Pair<Integer, Integer>
// like list,queue,etc;(Integer,Integer)
public static class Pair {
Integer node;
Integer w;
// Integer w;
public Pair(Integer node, Integer w) {
this.node = node;
this.w = w;
// this.w = w ;
}
}
// Pair class of Generic type
// static class Pair<A, B> {
// A first;
// B second;
// Constructor
// public Pair(A first, B second) {
// this.first = first;
// this.second = second;
// }
// }
/********************************************************************* */
// Fast Reader Class
public static class FastScanner {
// Reader object
BufferedReader reader;
public int[] intarr;
public Long[] longarr;
public Float[] floatarr;
public Double[] doublearr;
int sum;
long longsum;
float floatsum;
Double doublesum;
// Constructor
public FastScanner() {
// Initialize the reader
reader = new BufferedReader(new InputStreamReader(System.in));
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
} catch (Exception e) {
}
}
}
// String tokenizer
StringTokenizer tokenizer;
// Function to read a
// single integer
// to extend the fast reader class writer your function here
public int readInt() throws IOException {
return Integer.parseInt(reader.readLine());
}
// Function to read a
// single long
public long readLong() throws IOException {
return Long.parseLong(reader.readLine());
}
// Function to read string
public String readString() throws IOException {
return reader.readLine();
}
// Function to read a array
// of numsInts integers
// in one line
public int[] readIntArray(int n) throws IOException {
sum = 0;
intarr = new int[n];
tokenizer = new StringTokenizer(reader.readLine());
for (int i = 0; i < n; i++) {
sum += intarr[i] = Integer.parseInt(tokenizer.nextToken());
}
return intarr;
}
public Float[] readfloatArray(int n) throws IOException {
floatsum = 0f;
floatarr = new Float[n];
tokenizer = new StringTokenizer(reader.readLine());
for (int i = 0; i < n; i++) {
floatsum += floatarr[i] = Float.parseFloat(tokenizer.nextToken());
}
return floatarr;
}
public Double[] readDoubleArray(int n) throws IOException {
doublesum = 0d;
doublearr = new Double[n];
tokenizer = new StringTokenizer(reader.readLine());
for (int i = 0; i < n; i++) {
doublesum += doublearr[i] = Double.parseDouble(tokenizer.nextToken());
}
return doublearr;
}
public Long[] readLongArray(int n) throws IOException {
tokenizer = new StringTokenizer(reader.readLine());
longsum = 0L;
longarr = new Long[n];
int i = 0;
while (tokenizer.hasMoreTokens()) {
longsum += longarr[i] = Long.parseLong(tokenizer.nextToken());
i++;
}
return longarr;
}
public List<Integer> readIntAsList() throws IOException {
List<Integer> list = new ArrayList<Integer>();
tokenizer = new StringTokenizer(reader.readLine());
while (tokenizer.hasMoreTokens()) {
list.add(Integer.parseInt(tokenizer.nextToken()));
}
return list;
}
public List<Long> readLongAsList() throws IOException {
List<Long> list = new ArrayList<Long>();
tokenizer = new StringTokenizer(reader.readLine());
while (tokenizer.hasMoreTokens()) {
list.add(Long.parseLong(tokenizer.nextToken()));
}
return list;
}
}
// Fast Writer Class
public static class FastOutput {
// Writer object
BufferedWriter writer;
// Constructor
public FastOutput() {
// Initialize the writer
writer = new BufferedWriter(new OutputStreamWriter(System.out));
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
} catch (Exception e) {
}
}
}
// Function to write the
// single integer
public void writeInt(int i) throws IOException {
writer.write(Integer.toString(i));
writer.newLine();
writer.flush();
}
// Function to writer a single character
public void writeChar(char c) throws IOException {
writer.write(Character.toString(c));
writer.newLine();
writer.flush();
}
// Function to write single long
public void writeLong(long i) throws IOException {
writer.write(Long.toString(i));
writer.newLine();
writer.flush();
}
// Function to write String
public void writeString(String s) throws IOException {
writer.write(s);
writer.newLine();
writer.flush();
}
public void writeStringWithSpace(String s) throws IOException {
writer.write(s);
writer.write(" ");
writer.flush();
}
// Function to write a Integer of
// array with spaces in one line
public void writeIntArray(int[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
// Function to write Integer of
// array without spaces in 1 line
public void writeIntArrayWithoutSpaces(int[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(Integer.toString(nums[i]));
}
writer.newLine();
writer.flush();
}
public void writeIntegerlist(List<Integer> num) throws IOException {
if (num != null) {
for (Integer integer : num) {
writer.write(integer + " ");
}
}
writer.newLine();
writer.flush();
}
public void writeintmatrix(int[][] matrix) throws IOException {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
writer.write(matrix[i][j] + " ");
}
writer.newLine();
}
writer.flush();
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
662a791b7669c79de6086bb37171b11e
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Template {
static int mod = 1000000007;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int yo = sc.nextInt();
while (yo-- > 0) {
int n = sc.nextInt();
int[] a = sc.readArray(n);
if(isSorted(a)) {
out.println(0);
}
else {
if(a[0] == 1 || a[n-1] == n) {
out.println(1);
}
else if(a[0] == n && a[n-1] == 1){
out.println(3);
}
else {
out.println(2);
}
}
}
out.close();
}
private static int op2(int[] a, int n) {
Arrays.sort(a,0, n-1);
if(isSorted(a)) {
return 1;
}
Arrays.sort(a,1, n);
if(isSorted(a)) {
return 2;
}
else {
return 3;
}
}
static int op1(int[] a, int n) {
Arrays.sort(a,1, n);
if(isSorted(a)) {
return 1;
}
Arrays.sort(a,0, n-1);
if(isSorted(a)) {
return 2;
}
else {
return 3;
}
}
static boolean isSorted(int[] a) {
boolean ok = true;
for(int i = 1; i < a.length; i++) {
if(a[i] < a[i-1]) {
ok = false;
}
}
return ok;
}
static boolean isRevSorted(int[] a) {
boolean ok = true;
for(int i = 1; i < a.length; i++) {
if(a[i] > a[i-1]) {
ok = false;
}
}
return ok;
}
static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n), temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int n) {
boolean isPrime[] = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (isPrime[i])
continue;
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = true;
}
}
return isPrime;
}
static long pow(int a, long b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
if (b % 2 == 0) {
long ans = pow(a, b / 2);
return ans * ans;
} else {
long ans = pow(a, (b - 1) / 2);
return a * ans * ans;
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
0f65979354623a2562b83f86f656f48f
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Template {
static int mod = 1000000007;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int yo = sc.nextInt();
while (yo-- > 0) {
int n = sc.nextInt();
int[] a = sc.readArray(n);
if(isSorted(a)) {
out.println(0);
}
else {
if(a[0] == 1 || a[n-1] == n) {
out.println(1);
}
else {
int[] a1 = a.clone();
int op1 = op1(a1,n);
int op2 = op2(a,n);
out.println(Math.min(op1, op2));
}
}
}
out.close();
}
private static int op2(int[] a, int n) {
Arrays.sort(a,0, n-1);
if(isSorted(a)) {
return 1;
}
Arrays.sort(a,1, n);
if(isSorted(a)) {
return 2;
}
else {
return 3;
}
}
static int op1(int[] a, int n) {
Arrays.sort(a,1, n);
if(isSorted(a)) {
return 1;
}
Arrays.sort(a,0, n-1);
if(isSorted(a)) {
return 2;
}
else {
return 3;
}
}
static boolean isSorted(int[] a) {
boolean ok = true;
for(int i = 1; i < a.length; i++) {
if(a[i] < a[i-1]) {
ok = false;
}
}
return ok;
}
static boolean isRevSorted(int[] a) {
boolean ok = true;
for(int i = 1; i < a.length; i++) {
if(a[i] > a[i-1]) {
ok = false;
}
}
return ok;
}
static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n), temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int n) {
boolean isPrime[] = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (isPrime[i])
continue;
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = true;
}
}
return isPrime;
}
static long pow(int a, long b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
if (b % 2 == 0) {
long ans = pow(a, b / 2);
return ans * ans;
} else {
long ans = pow(a, (b - 1) / 2);
return a * ans * ans;
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
51dc4ea0c2c618b12ad44529f335bcc6
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st=new StreamTokenizer(br);
static int nextInt() throws IOException
{
st.nextToken();
return (int)st.nval;
}
static String nextStr() throws IOException
{
st.nextToken();
return st.sval;
}
public static void main(String[] args) throws IOException
{
int T=nextInt();
while(T-->0)
{
int n=nextInt();
int[] nums=new int[n];
int j=1;
for(int i=0; i<n; i++)
{
nums[i]=nextInt();
if(nums[i]==j)
j++;
}
if(j>n)
{
System.out.println(0);
continue;
}
int pos1=-1, pos2=-1;
for(int i=0; i<n; i++)
{
if(nums[i]==1)
pos1=i;
if(nums[i]==n)
pos2=i;
}
if(pos1==0)
System.out.println(1);
else if(pos1==n-1)
{
if(pos2==0)
System.out.println(3);
else
System.out.println(2);
}
else
{
System.out.println(pos2==n-1 ? 1 : 2);
}
}
}
}
class HideArray
{
int[] preSum;
HideArray(int[] nums)
{
int n=nums.length;
preSum=new int[n+1];
for(int i=1; i<=n; i++)
preSum[i]=preSum[i-1]+nums[i-1];
}
int getPreSum(int right)
{
return preSum[right];
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
e7c7b9cfa786c3f5ba0c6a4eae1941f0
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class PermutationSort {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int t= sc.nextInt();
while(t>0){
int n = sc.nextInt();
int[] arr=new int[n];
int temp=0;
for(int i=0;i<n;i++){
arr[i]= sc.nextInt();
if(arr[i]!=i+1)
temp+=1;
}
if(temp==0)
System.out.println(0);
else if(arr[0]==1 || arr[n-1]==n)
System.out.println(1);
else if(arr[0]==n && arr[n-1]==1)
System.out.println(3);
else
System.out.println(2);
t--;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
2cfa12b0cdddbee9a05758d5c73e5f22
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.math.*;
import java.io.*;
public class A{
static FastReader scan=new FastReader();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static LinkedList<Integer>edges[];
// static LinkedList<Pair>edges[];
static boolean stdin = true;
static String filein = "input";
static String fileout = "output";
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
int dx_8[]={1,1,1,0,0,-1,-1,-1};
int dy_8[]={-1,0,1,-1,1,-1,0,1};
static char sts[]={'U','R','D','L'};
static boolean prime[];
static long LCM(long a,long b){
return (Math.abs(a*b))/gcd(a,b);
}
public static int upperBound(long[] array, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = low+(high-low) / 2;
if ( array[mid]>value) {
high = mid ;
} else {
low = mid+1;
}
}
return low;
}
static long gcd(long a, long b) {
if(a!=0&&b!=0)
while((a%=b)!=0&&(b%=a)!=0);
return a^b;
}
static int countSetBits(int n)
{
int count = 0;
while (n > 0) {
if((n&1)!=1)
count++;
//count += n & 1;
n >>= 1;
}
return count;
}
static void sieve(long n)
{
prime = new boolean[(int)n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
static boolean isprime(long x)
{
for(long i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
static int perm=0,FOR=0;
static boolean flag=false;
static int len=100000000;
static ArrayList<Pair>inters=new ArrayList<Pair>();
static class comp1 implements Comparator<Pair>{
public int compare(Pair o1,Pair o2){
return Integer.compare((int)o2.x,(int)o1.x);
}
}
public static class comp2 implements Comparator<Pair>{
public int compare(Pair o1,Pair o2){
return Integer.compare((int)o2.x,(int)o1.x);
}
}
static StringBuilder a,b;
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static ArrayList<Integer>v;
static ArrayList<Integer>pows;
static void block(long x)
{
v = new ArrayList<Integer>();
pows=new ArrayList<Integer>();
while (x > 0)
{
v.add((int)x % 2);
x = x / 2;
}
// Displaying the output when
// the bit is '1' in binary
// equivalent of number.
for (int i = 0; i < v.size(); i++)
{
if (v.get(i)==1)
{
pows.add(i);
}
}
}
static long ceil(long a,long b)
{
if(a%b==0)
return a/b;
return a/b+1;
}
static int n,m;
static long arr[];
static long ans=0;
static boolean vis[]=new boolean[21];
static int c[]=new int[21];
static ArrayList<Integer>comps[];
static boolean isprime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to return the smallest
// prime number greater than N
static int nextPrime(int N)
{
// Base case
if (N <= 1)
return 2;
int prime = N;
boolean found = false;
// Loop continuously until isPrime returns
// true for a number greater than n
while (!found)
{
prime++;
if (isprime(prime))
found = true;
}
return prime;
}
static long dp[][][];
static long mod=(long)1e9+7;
static int mx=0,k;
static long nPr(long n,long r)
{
long ret=1;
for(long i=n-r+1;i<=n;i++)
{
ret=1L*ret*i%mod;
}
return ret%mod;
}
static Set<Long>set;
static TreeMap<Long,Integer>tmap;
static long pre[];
static void rec(int l,int r)
{
if(l>=r){
if(l==r)
set.add(arr[l]);
return ;
}
//System.out.println(l+" "+r );
//System.out.println(pre[r]+" "+r);
set.add(pre[r]-pre[l-1]);
long mid=(arr[r]+arr[l])/2;
// System.out.println("MID "+mid);
long id=tmap.floorKey(mid);
// System.out.println(id);
id=tmap.get(id);
//System.out.println(l+" "+id);
if(id!=r){
rec(l,(int)id);
}
//else out.println("FUCK");
if(id+1!=l)
rec((int)(id+1),r);
}
public static void main(String[] args) throws Exception
{
//SUCK IT UP AND DO IT ALRIGHT
//scan=new FastReader("hps.in");
//out = new PrintWriter("hps.out");
//System.out.println( 1005899102^431072812);
//int elem[]={1,2,3,4,5};
//System.out.println("avjsmlfpb".compareTo("avjsmbpfl"));
int tt=1;
/*for(int i=0;i<=100;i++)
if(prime[i])
arr.add(i);
System.out.println(arr.size());*/
// check(new StringBuilder("05:11"));
// System.out.println(26010000000000L%150);
//System.out.println((1000000L*99000L));
tt=scan.nextInt();
// System.out.println(2^6^4);
//StringBuilder o=new StringBuilder("GBGBGG");
//o.insert(2,"L");
int T=tt;
//System.out.println(gcd(3,gcd(24,gcd(120,168))));
//System.out.println(gcd(40,gcd(5,5)));
//System.out.println(gcd(45,gcd(10,5)));
outer:while(tt-->0)
{
int i=0;
int n=scan.nextInt();
int arr[]=new int[n];
for( ;i<n;i++)
{
arr[i]=scan.nextInt();
}
i=1;
//out.println(arr[0]);
while(i<=n&&arr[i-1]==i)
i++;
//out.println(i);
if(i==n+1)
{
out.println(0);
continue outer;
}
if(i>1)
{
out.println(1);
continue outer;
}
i=n;
while(i>=0&&arr[i-1]==i)
i--;
if(i<n)
{
out.println(1);
continue outer;
}
i=0;
ArrayList<Integer>first=new ArrayList<Integer>();
that:for(;i<n-1;i++)
{
int tmp[]=new int[n];
for(int k=0;k<n;k++)
tmp[k]=arr[k];
Arrays.sort(tmp,0,i+1);
//first.add(arr[i]);
int j=1;
while(j<=n&&tmp[j-1]==j)
j++;
if(j>1)
{
out.println(2);
continue outer;
}
j=n;
while(j>=0&&tmp[j-1]==j)
j--;
if(j<n)
{
out.println(2);
continue outer;
}
}
i=1;
that:for(;i<n;i++)
{
int tmp[]=new int[n];
for(int k=0;k<n;k++)
tmp[k]=arr[k];
Arrays.sort(tmp,1,i+1);
//first.add(arr[i]);
int j=1;
while(j<=n&&tmp[j-1]==j)
j++;
if(j>1)
{
//for(int k=0;k<n;k++)
// out.print(tmp[k]+" ");
// out.println();
// out.println(i);
//out.println("FUCK");
out.println(2);
continue outer;
}
j=n;
while(j>=0&&tmp[j-1]==j)
j--;
if(j<n)
{
out.println(2);
continue outer;
}
}
out.println(3);
}
out.close();
//SEE UP
}
static class special implements Comparable<special>{
int x,y,z,h;
String s;
special(int x,int y,int z,int h)
{
this.x=x;
this.y=y;
this.z=z;
this.h=h;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
special t = (special)o;
return t.x == x && t.y == y&&t.s.equals(s);
}
public int compareTo(special o)
{
return Integer.compare(x,o.x);
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return (base % mod+mod)%mod;
long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return (base * R % mod+mod)%mod;
}
else return (R %mod+mod)%mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
//Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private static void sort2(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastReader(String filename)throws Exception
{
br=new BufferedReader(new FileReader(filename));
}
boolean hasNext(){
String line;
while(root.hasMoreTokens())
return true;
return false;
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class Pair implements Comparable<Pair>{
public long x, y;
public Pair(long x1, long y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y;
}
public int compareTo(Pair o)
{
return (int)(o.x-x);
}
}
static class tuple{
int x,y,z;
tuple(int a,int b,int c){
x=a;
y=b;
z=c;
}
}
static class Edge{
int d,w;
Edge(int d,int w)
{
this.d=d;
this.w=w;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
f2ec771e37935d469b1a8f86b63e4d66
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.IOException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args){
FastReader fastReader = new FastReader();
long t = fastReader.nextLong();
while (t-->0){
long n = fastReader.nextLong();
ArrayList<Long> array = new ArrayList<>();
ArrayList<Long> sortedArray = new ArrayList<>();
ArrayList<Long> reverseArray = new ArrayList<>();
long min = 0 , max = 0;
for(long i=0 ; i<n ; i++){
long element = fastReader.nextLong();
array.add(element);
sortedArray.add(element);
reverseArray.add(element);
}
Collections.sort(sortedArray);
Collections.sort(reverseArray);
Collections.reverse(reverseArray);
min = sortedArray.get(0);
max = sortedArray.get(sortedArray.size()-1);
if(sortedArray.equals(array)){
System.out.println(0);
}
else if(min== array.get(array.size()-1) && max== array.get(0)) {
System.out.println(3);
}
else if(min == array.get(0) || max == array.get(array.size()-1)){
System.out.println(1);
}
else {
System.out.println(2);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
7a012a87a6cc7944154a18ded01d51d4
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
public class Main {
// private static int dp[][][];
// private static HashMap<String, Integer> dp = new HashMap<>();
// add all number which has its any place as zero plus one no zero any number
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
int cases = ip.nextInt();
while (cases > 0) {
int n = ip.nextInt();
int ar[] = new int[n];
for (int i=0; i<n; i++) {
ar[i] = ip.nextInt();
}
System.out.println(getSteps(ar));
cases--;
}
}
private static int getSteps(int[] ar) {
// System.out.println("in...");
if (checkSorted(ar)) return 0;
int n = ar.length;
if (ar[0] == n && ar[n-1] == 1) {
return 3;
} else if (ar[0] == 1 || ar[n-1] == n) {
return 1;
} else {
return 2;
}
// for (int i=0; i<n; i++) {
// if (ar[i] != sr[i]) {
// flag = true;
// }
// }
// int count = 0;
// while (flag) {
// int pos = 0;
// for (int i=0; i<n; i++) {
// if (ar[i] != i+1) {
// pos = i;
// break;
// }
// }
// // System.out.println("pos:: "+pos);
// if (pos == 0) {
// int[] tmp = new int[n-1];
// for (int i=0; i<n-1; i++) {
// tmp[i] = ar[i];
// }
// Arrays.sort(tmp);
// for (int i=0; i<n-1; i++) {
// ar[i] = tmp[i];
// }
// count++;
// if (checkSorted(ar)) {
// return count;
// }
// } else {
// int[] tmp = new int[n-1];
// for (int i=1; i<n; i++) {
// tmp[i-1] = ar[i];
// }
// Arrays.sort(tmp);
// for (int i=0; i<n-1; i++) {
// ar[i+1] = tmp[i];
// }
// count++;
// if (checkSorted(ar)) {
// return count;
// }
// }
// }
//return 0;
}
private static boolean checkSorted(int[] ar) {
// for(int i=0; i<ar.length; i++) {
// System.out.print(ar[i]+", ");
// }
// System.out.println();
for (int i=0; i<ar.length-1; i++) {
if (ar[i]>ar[i+1])
return false;
}
return true;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
d4fa2bc09d857698e842434467502245
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class CodeChef
{
public static void main (String[] args) throws java.lang.Exception
{
FastReader ob = new FastReader();
int t = ob.nextInt();
while (t-- > 0) {
int n = ob.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ob.nextInt();
if(isSorted(a))
System.out.println(0);
else if(a[0]==n && a[n-1] == 1)
System.out.println(3);
else if(a[0] == 1 || a[n-1]==n)
System.out.println(1);
else
System.out.println(2);
}
}
static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static long sum(long a ){
long s =0;
while(a>0){
s+=a%10;
a=a/10;
}
return s;
}
public static int sum(int a ){
int s =0;
while(a>0){
s+=a%10;
a=a/10;
}
return s;
}
public static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static int LCM(int a, int b)
{
return (a / gcd(a, b)) * b;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
97e5cd79d9fc779722f8743db8741463
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(), "Main", 1 << 27).start();
}
static class Pair {
int f;
int s;
int p;
PrintWriter w;
// int t;
Pair(int f, int s) {
// Pair(int f,int s, PrintWriter w){
this.f = f;
this.s = s;
// this.p = p;
// this.w = w;
// this.t = t;
}
public static Comparator<Pair> wc = new Comparator<Pair>() {
public int compare(Pair e1,Pair e2){
// 1 for swap
if(Math.abs(e1.f)-Math.abs(e2.f)!=0){
// e1.w.println("**"+e1.f+" "+e2.f);
return (Math.abs(e1.f)-Math.abs(e2.f));
}
else{
// e1.w.println("##"+e1.f+" "+e2.f);
return (Math.abs(e1.s)-Math.abs(e2.s));
}
}
};
}
public Integer[] sort(Integer[] a) {
Arrays.sort(a);
return a;
}
public Long[] sort(Long[] a) {
Arrays.sort(a);
return a;
}
public static ArrayList<Integer> sieve(int N) {
int i, j, flag;
ArrayList<Integer> p = new ArrayList<Integer>();
for (i = 1; i < N; i++) {
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
p.add(i);
}
}
return p;
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
//// recursive dfs
public static int dfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] v, PrintWriter w, int p) {
v[s] = true;
int ans = 1;
// int n = dist.length - 1;
int t = g[s].size();
// int max = 1;
for (int i = 0; i < t; i++) {
int x = g[s].get(i);
if (!v[x]) {
// dist[x] = dist[s] + 1;
ans = Math.min(ans, dfs(x, g, dist, v, w, s));
} else if (x != p) {
// w.println("* " + s + " " + x + " " + p);
ans = 0;
}
}
// max = Math.max(max,(n-p));
return ans;
}
//// iterative BFS
public static int bfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] b, PrintWriter w, int p) {
b[s] = true;
int siz = 1;
// dist--;
Queue<Integer> q = new LinkedList<>();
q.add(s);
while (q.size() != 0) {
int i = q.poll();
Iterator<Integer> it = g[i].listIterator();
int z = 0;
while (it.hasNext()) {
z = it.next();
if (!b[z]) {
b[z] = true;
// dist--;
dist[z] = dist[i] + 1;
// siz++;
q.add(z);
} else if (z != p) {
siz = 0;
}
}
}
return siz;
}
public static int lower(int a[], int x) { // x is the target value or key
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
public static int upper(int a[], int x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
public static int lower(ArrayList<Integer> a, int x) { // x is the target value or key
int l = -1, r = a.size();
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a.get(m) >= x)
r = m;
else
l = m;
}
return r;
}
public static int upper(ArrayList<Integer> a, int x) {// x is the key or target value
int l = -1, r = a.size();
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a.get(m) <= x)
l = m;
else
r = m;
}
return l + 1;
}
public static long power(long x, long y, long m) {
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
public void yesOrNo(boolean f) {
if (f) {
w.println("YES");
} else {
w.println("NO");
}
}
// Arrays.sort(rooms, (room1, room2) -> room1[1] == room2[1] ? room1[0]-room2[0]
// : room2[1]-room1[1]);
// Arrays.sort(A, (a, b) -> Integer.compare(a[0] , b[0]));
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// code here
int oo = (int) 1e9;
int[] parent;
int[] dist;
int[] height;
boolean[] vis;
// ArrayList<Integer>[] g;
// int[] col;
// HashMap<Long, Boolean>[] dp;
//char[][] g;
// boolean[][] v;
Long[] a;
// ArrayList<Integer[]> a;
// int[][] ans;
long[][] dp;
long mod;
int n;
int m;
int k;
long[][] pre;
// StringBuilder[] a;
// StringBuilder[] b;
// StringBuilder ans;
int[][] col;
int[][] row;
PrintWriter w = new PrintWriter(System.out);
public void run() {
InputReader sc = new InputReader(System.in);
int defaultValue = 0;
mod = 1000000007;
int test = 1;
test = sc.nextInt();
while (test-- > 0) {
n = sc.nextInt();
int[] a = new int[n];
boolean f = true;
boolean z = true;
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
if(i!=a[i]-1)f = false;
//if(i!=(n-a[i]))z = false;
}
int ans = 2;
if(a[0]==1 || a[n-1]==n)ans = 1;
else if(a[0]==n && a[n-1]==1) ans = 3;
if(f)ans = 0;
w.println(ans);
}
w.flush();
w.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
506eebf31273ea9cc227cf16fbad0552
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Valid_Path {
static int mod=(int)(1e9+7);
public static int gcd(int a,int b){
if(a==0)
return b;
else
return gcd(b%a,a);
}
public static void main(String arg[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(s[i]);
}
int cou=0;
int j=0;
for(int i=0;i<n;i++){
if(a[i]!=i+1)
cou++;
}
if(cou>0){
if(a[0]==1||a[n-1]==n)
sb.append("1\n");
else if(a[0]==n&&a[n-1]==1)
sb.append("3\n");
else
sb.append("2\n");
}
else
sb.append("0\n");
}
System.out.println(sb);
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
1ccbb28a521a5afe91c648eb9e7632f7
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class A{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static int mul(int a,int b){
return (a*b)%mod;
}
static int add(int a,int b){
return (a+b)%mod;
}
static int nC2(int n){
return mul(n,n-1)/2;
}
static int mod = (int)(1e9)+7;
public static void main(String args[]){
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
boolean isSorted = true;
boolean[] isSortedLeft = new boolean[n];
boolean[] isSortedRight = new boolean[n];
isSortedLeft[0] = true;
isSortedRight[n-1] = true;
for(int i=1;i<n;i++){
if(a[i] < a[i-1]){
isSorted = false; break;
}
}
if(isSorted){
out.println(0); continue;
}
int min1 = Integer.MAX_VALUE;
int min2 = min1;
int max1 = Integer.MIN_VALUE;
int max2 = max1;
for(int i=0;i<n;i++){
if(a[i] > max1){
max2 = max1;
max1 = a[i];
}
else if(a[i] > max2){
max2 = a[i];
}
}
for(int i=1;i<n;i++){
if(a[i] < min1){
min2 = min1;
min1 = a[i];
}
else if(a[i] < min2){
min2 = a[i];
}
}
if(a[0] == max1 && a[n-1] == min1){
out.println(3);
continue;
}
int[] preMax = new int[n];
int[] sufMin = new int[n];
preMax[0] = a[0];
sufMin[n-1] = a[n-1];
for(int i=1;i<n;i++){
int j = n-1-i;
if(a[i] < a[i-1]){
isSortedLeft[i] = false;
}
else isSortedLeft[i] = isSortedLeft[i-1];
if(a[j] > a[j+1]){
isSortedRight[j] = false;
}
else isSortedRight[j] = isSortedRight[j+1];
preMax[i] = Math.max(preMax[i-1],a[i]);
sufMin[j] = Math.min(sufMin[j+1],a[j]);
}
boolean ok = false;
for(int i=0;i<n-1;i++){
int j = n-1-i;
if(isSortedRight[i+1] && preMax[i] < a[i+1]){
ok = true; break;
}
if(isSortedLeft[i] && sufMin[i+1] > a[i]){
ok = true; break;
}
}
if(ok){
out.println(1); continue;
}
// if((a[0] > min1 && a[0] < min2) || (a[n-1] < max1 && a[n-1] > max2)){
// out.println(2); continue;
// }
out.println(2);
}
out.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ccc121a3b4d66ce8bf5045418171efe9
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class GFG {
public static void main (String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int[] arr=new int[n];
int c=0;
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
if(arr[i]!=i+1)
c++;
}
if(c==0)
pw.println("0");
else if(arr[0]==n && arr[n-1]==1)
pw.println("3");
else if(arr[0]==1 || arr[n-1]==n)
pw.println("1");
else if(c==2)
pw.println("1");
else
pw.println("2");
}
pw.flush();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
064024b092091a56474d2277af6d0333
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
/*
author : Multi-Thread
*/
public class B {
// static int INF = 998244353;
static int INF = (int) 1e9 + 7;
static int MAX = Integer.MAX_VALUE;
static int MIN = Integer.MIN_VALUE;
public static void main(String[] args) {
int test = fs.nextInt();
// int test = 1;
for (int cases = 0; cases < test; cases++) {
// solve();
System.out.println(solve());
}
}
static String solve() {
int n = fs.nextInt();
int ar[] = new int[n];
int cp[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
cp[i] = i + 1;
}
if (Arrays.equals(cp, ar))
return 0 + "";
if (ar[0] == 1 || ar[n - 1] == n) {
return 1 + "";
}
if (ar[0] == n && ar[n - 1] == 1)
return 3 + "";
return 2 + "";
}
static int _min(int... ar) {
intSort(ar, true);
return ar[ar.length - 1];
}
static int _max(int... ar) {
intSort(ar, true);
return ar[ar.length - 1];
}
static void intSort(int[] a, boolean reverse) {
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i : a)
al.add(i);
Collections.sort(al);
if (reverse) {
for (int i = 0; i < a.length; i++)
a[i] = al.get(a.length - i - 1);
} else {
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
}
static void longSort(long[] a, boolean reverse) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a)
al.add(i);
Collections.sort(al);
if (reverse) {
for (int i = 0; i < a.length; i++)
a[i] = al.get(a.length - i - 1);
} else {
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
}
public static int[] radixSort(int[] f) {
return radixSort(f, f.length);
}
public static int[] radixSort(int[] f, int n) {
int[] to = new int[n];
{
int[] b = new int[65537];
for (int i = 0; i < n; i++)
b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++)
b[i] += b[i - 1];
for (int i = 0; i < n; i++)
to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < n; i++)
b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++)
b[i] += b[i - 1];
for (int i = 0; i < n; i++)
to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public String toString() {
return "[" + first + "," + second + "]";
}
}
static class LongPair {
long first;
long second;
LongPair(long a, long b) {
this.first = a;
this.second = b;
}
public String toString() {
return "[" + first + "," + second + "]";
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void println() {
writer.print("\n");
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final FastReader fs = new FastReader();
private static final OutputWriter out = new OutputWriter(System.out);
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
856c8f3aca74967844b418656829640a
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class B_Permutation_Sort{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = scan.nextInt();
while(testCases-->0){
int length = scan.nextInt();
int[] vals = new int[length];
int largest = -1;
int min = Integer.MAX_VALUE;
boolean sorted = true;
int lastVal = -1;
for(int k =0;k<length;k++){
vals[k] = scan.nextInt();
if(largest<vals[k]){
largest = vals[k];
}
if(min>vals[k]){
min = vals[k];
}
if(lastVal>vals[k]){
sorted = false;
}
lastVal = vals[k];
}
int result = 0;
if(sorted){
result = 0;
}else if(vals[0]!=min && vals[length-1]!=largest){
result = 2;
}else{
result = 1;
}
if(vals[0]==largest && vals[length-1]==min){
result = 3;
}
System.out.println(result);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
72c09de342c93b1e914274cf6dcdcef7
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class B_Permutation_Sort{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = scan.nextInt();
while(testCases-->0){
int length = scan.nextInt();
int[] vals = new int[length];
int largest = -1;
int min = Integer.MAX_VALUE;
boolean sorted = true;
int lastVal = -1;
for(int k =0;k<length;k++){
vals[k] = scan.nextInt();
if(largest<vals[k]){
largest = vals[k];
}
if(min>vals[k]){
min = vals[k];
}
if(lastVal>vals[k]){
sorted = false;
}
lastVal = vals[k];
}
int result = 0;
if(sorted){
result = 0;
}else if(vals[0]==largest || vals[length-1]==min){
result = 2;
}else if(vals[0]!=min && vals[length-1]!=largest){
result = 2;
}else{
result = 1;
}
if(vals[0]==largest && vals[length-1]==min){
result = 3;
}
System.out.println(result);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
bbb09ea5e2c5d78378b11dec5f83f73a
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Two {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while (t!=0){
int n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int co=0;int max=a[0];
int min=a[0];
for(int i=0;i<n;i++){
if(i<n-1&&a[i]>a[i+1]){
co++;
}
}
if(co==0){
System.out.println("0");
}else if(co+1==n){
System.out.println("3");
}
else {
if(a[0]==n&&a[n-1]==1){
System.out.println("3");
}
else {
if(a[0]==1||a[n-1]==n){
System.out.println("1");
}
else {
System.out.println("2");
}
}
}
t--;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
7ae034f17a2813b922b97a17b619f10b
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution{
//fast input output method
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String [] args) throws Exception {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader sc =new FastReader();
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int a[] = new int[n];
int check=1;
boolean sort = true;
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
if(a[i]!=check)
{
sort = false;
}
check++;
}
int start = a[0];
int end = a[n-1];
if(sort)
out.println(0);
else
{
if(start==1 || end==n)
out.println(1);
else if(start==n && end==1)
out.println(3);
else
out.println(2);
}
}
out.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
ccb464b0239ecd319a460219e048ffd8
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t>0){
int n = input.nextInt();
int[] arr = new int[n];
int[] brr = new int[n];
for(int i=0;i<n; i++){
arr[i] = input.nextInt();
brr[i]=arr[i];
}
int count=0;
Arrays.sort(brr);
for(int i=0; i<n; i++){
if(arr[i]==brr[i])
count++;
}
if(count==n)
System.out.println("0");
else
if(brr[0]==arr[0] || brr[n-1]==arr[n-1])
System.out.println("1");
else
if(arr[0]==brr[n-1]&&brr[0]==arr[n-1])
System.out.println("3");
else
System.out.println("2");
t--;
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
c742cd5216fc64869ac15db96a5af8ae
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
//package com.codewithak;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static Scanner myObj = new Scanner(System.in);
public static void main(String[] args) {
// write your code here
// System.out.println("hello world");
// Scanner myObj = new Scanner(System.in); // Create a Scanner object
// System.out.print(1);
long t = myObj.nextLong();
while(t-- > 0){
solve();
}
return ;
}
public static void solve(){
int n = myObj.nextInt() , i=0, flag=0 , cnt=0;
int[] arr = new int[n];
for(i=0;i<n;i++){
int x = myObj.nextInt();
arr[i] = x;
}
int b[] = arr.clone();
Arrays.sort(b);
for(i=0;i<n;i++){
if(arr[i] != b[i])
cnt++;
}
// System.out.println(arr[0] + " " + b[0] + arr[n-1] + " " + b[n-1]);
if(arr[0] != b[0] && arr[n-1] != b[n-1]){
if(arr[0] == b[n-1] && arr[n-1] == b[0]){
System.out.println(3);
return ;
}else{
System.out.println(2);
return ;
}
}
else if(cnt == 0){
System.out.println(0);
return ;
}
System.out.println(1);
return ;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
89b4aec460be1b0fb7a68a4958d8c257
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.Math.PI;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
import static java.lang.System.exit;
import static java.util.Arrays.copyOf;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Comparator;
import java.lang.StringBuilder;
import java.util.Collections;
import java.text.DecimalFormat;
public class Solution {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
private static void solve() throws IOException{
int n=scanInt();
int arr[]=new int[n];
for(int i=0; i<n; ++i)
arr[i]=scanInt();
if(checkAsc(arr)){
out.println(0);
return ;
}
if(arr[0]==1 || arr[n-1]==n){
out.println(1);
return;
}
if(arr[0]==n && arr[n-1]==1){
out.println(3);
return;
}
out.println(2);
}
private static boolean checkAsc(int arr[]){
for(int i=1; i<arr.length; ++i){
if(arr[i-1]>arr[i])
return false;
}
return true;
}
public static void main(String[] args) {
try {
long startTime = System.currentTimeMillis();
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
int test=scanInt();
for(int t=1; t<=test; t++){
//out.print("Case #"+t+": ");
solve();
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
//System.out.println(totalTime+" "+System.currentTimeMillis() );
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static double scanDouble() throws IOException {
return parseDouble(scanString());
}
static String scanString() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String scanLine() throws IOException {
return in.readLine();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
48a68a29535e315b971cd89d1ccc45cd
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static HashMap<String,Integer>map=new HashMap<>();
public static void main(String args[] ) throws Exception {
FastReader s=new FastReader();
int t=s.nextInt();
long mod=(long) (1e9+7);
while(t-->0)
{
int n=s.nextInt();
int[] arr=new int[n];
boolean flag=true;
for(int i=1;i<=n;i++)
{
arr[i-1]=s.nextInt();
if(arr[i-1]!=i)flag=false;
}
if(flag)System.out.println(0);
else if(arr[0]==1||arr[n-1]==n)System.out.println(1);
else if(arr[0]!=n||arr[n-1]!=1)System.out.println(2);
else if(arr[0]!=1||arr[n-1]!=n)System.out.println(3);
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
9d12b86bdc9b7071a7f66af123ed8916
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class A{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0){
int n = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = s.nextInt();
}
int ans = solve(arr, n);
System.out.println(ans);
}
}
public static int solve(int[] arr, int n){
int ct = 0;
for(int i = 0; i < n; i++){
if(arr[i] != i + 1) ct++;
}
if(ct == 0 ) return 0;
if(arr[0] == n && arr[n-1] == 1) return 3;
if(arr[0] == 1 || arr[n-1] == n) return 1;
return 2;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
3f157ec03b859afbf2766852570c592d
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
public class A {
static boolean isSorted(int arr[]){
for(int i=0; i<arr.length-1; i++){
if(arr[i]>arr[i+1]){
return false;
}
}
return true;
}
static int solve(int arr[], int n){
if(isSorted(arr)){
return 0;
}
else if(arr[0]==n && arr[n-1]==1){
return 3;
}
else if(arr[0]!=1 && arr[n-1]!=n){
return 2;
}
else{
return 1;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
System.out.println(solve(arr, n));
}
sc.close();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
aa705ba511f31e88de1d6d92fb6d5a11
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution {
private static final FastReader reader = new FastReader();
private static final PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
private static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void closeStreams() {
try {
reader.br.close();
} catch (IOException e) {
e.printStackTrace();
}
writer.close();
}
private static void solve() {
int n = reader.nextInt();
int first = reader.nextInt();
int prev = Integer.MIN_VALUE;
int largest = first;
int smallest = first;
boolean isSorted = true;
for (int i = 1; i < n; i++) {
int elem = reader.nextInt();
if (prev > elem)
isSorted = false;
if (largest < elem)
largest = elem;
if (smallest > elem)
smallest = elem;
prev = elem;
}
if (first == largest) {
if (prev == smallest) {
writer.println(3);
return;
}
writer.println(2);
return;
}
if (first == smallest) {
if (isSorted) {
writer.println(0);
return;
}
writer.println(1);
return;
}
if (prev == largest) {
writer.println(1);
return;
}
writer.println(2);
}
public static void main(String[] args) {
int tests = reader.nextInt();
while (tests-- != 0)
solve();
closeStreams();
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
9a4a37ae53838df8b411ea50afb718d0
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
/*
⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀⠀⢠⠞⠉⠙⠲⡀⠀
⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀⡏⠀⠀Y⠀⠀⢷
⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀⢸⠀⠀A⠀⠀ ⡇
⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀⣸⠀⠀S⠀ ⡇
⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀⢹⠀⠀H⠀⠀ ⡇
⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀⠀⡇⠀⠀⠀⠀⡼
⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀⠀⠘⠤⣄⣠⠞⠀
⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀
⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀
⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀
⣿⣿⣧⣀⣿………⣀⣰⣏⣘⣆⣀⠀⠀
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; // System.out is a PrintStream
// import java.util.Arrays;
// import java.util.ArrayDeque;
// import java.util.ArrayList;
// import java.util.Collections; // for sorting ArrayList mainly
// import java.util.HashMap;
// import java.util.HashSet;
// import java.util.Random;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
FastScanner scn = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
for (int tc = scn.nextInt(); tc > 0; tc--) {
int N = scn.nextInt();
int[] arr = new int[N];
boolean sorted = true;
for (int i = 0; i < N; i++) {
arr[i] = scn.nextInt();
if (i + 1 != arr[i]) sorted = false;
}
if (sorted) {
out.println("0");
} else if (arr[0] == N && arr[N - 1] == 1) {
out.println("3");
} else if (arr[0] != 1 && arr[N - 1] != N) {
out.println("2");
} else {
out.println("1");
}
}
out.close();
}
private static int gcd(int num1, int num2) {
int temp = 0;
while (num2 != 0) {
temp = num1;
num1 = num2;
num2 = temp % num2;
}
return num1;
}
private static int lcm(int num1, int num2) {
return (int)((1L * num1 * num2) / gcd(num1, num2));
}
private static void ruffleSort(int[] arr) {
// int N = arr.length;
// Random rand = new Random();
// for (int i = 0; i < N; i++) {
// int oi = rand.nextInt(N), temp = arr[i];
// arr[i] = arr[oi];
// arr[oi] = temp;
// }
// Arrays.sort(arr);
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() {
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
4122b9b3821c882d50166c407165a2bd
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class B_Permutation_Sort{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0){
int n = in.nextInt();
String str[] = in.nextLine().split(" ");
int []arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
int arr1[] = Arrays.copyOf(arr, n);
Arrays.sort(arr1);
if(Arrays.equals(arr, arr1)){
System.out.println(0);
continue;
}
else if(arr[0]==1 || arr[n-1]==n){
System.out.println(1);
continue;
}
else if(arr[0]==n && arr[n-1]==1){
System.out.println(3);
continue;
}
else{
System.out.println(2);
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
0e5ba815d76d6b8ed23c760e9a6337ca
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
input.nextLine();
for(int i=0; i<t; i++)
{
int n = input.nextInt();
input.nextLine();
int[] arr = new int[n];
for(int j=0; j<n; j++)
{
arr[j] = input.nextInt();
}
input.nextLine();
if(isSorted(arr))
{
System.out.println(0);
}
else if(arr[0] == 1 || arr[n-1] == n)
{
System.out.println(1);
}
else if(arr[0] == n && arr[n-1] == 1)
{
System.out.println(3);
}
else
{
System.out.println(2);
}
}
}
public static boolean isSorted(int[] ar)
{
for(int i=0; i<ar.length-1; i++)
{
if(ar[i] > ar[i+1])
{
return false;
}
}
return true;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
0f1edaa17af0aafc656e43d298c66ad8
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
//Scanner input = new Scanner(new File("psort.in"));
int t = input.nextInt();
input.nextLine();
for(int i=0; i<t; i++)
{
int n = input.nextInt();
input.nextLine();
int[] arr = new int[n];
for(int j=0; j<n; j++)
{
arr[j] = input.nextInt();
}
input.nextLine();
if(isSorted(arr))
{
System.out.println(0);
}
else if(arr[0] == 1 || arr[n-1] == n)
{
System.out.println(1);
}
else if(arr[0] == n && arr[n-1] == 1)
{
System.out.println(3);
}
else
{
System.out.println(2);
}
}
}
public static boolean isSorted(int[] ar)
{
for(int i=0; i<ar.length-1; i++)
{
if(ar[i] > ar[i+1])
{
return false;
}
}
return true;
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
61b633ee2a544ecbea1ac03aa7b83357
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.*;
// Compiler version JDK 11.0.2
public class Dcoder
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int[] a =new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int s=1,mn=0,mx=0;
for(int i=0;i<a.length;i++)
{
if(i>0 && a[i]>a[i-1])
s++;
if(a[mn]>a[i])
mn=i;
if(a[mx]<a[i])
mx=i;
}
if(s==a.length)
{
System.out.println("0");
}
else
{
if(mn==0 || mx==a.length-1)
System.out.println("1");
else if(mx==0 && mn==a.length-1)
System.out.println("3");
else
System.out.println("2");
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
af392ee3cb83eae59f857e34ebb252ee
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int a = sc.nextInt();
ArrayList<Integer> arrayList = new ArrayList<>();
for (int j = 0; j < a; j++) {
arrayList.add(sc.nextInt());
}
int count = 0;
if (arrayList.get(0) == a) {
if (arrayList.get(arrayList.size() - 1) == 1) {
System.out.println(3);
} else {
System.out.println(2);
}
} else if (arrayList.get(0) != 1) {
if (arrayList.get(arrayList.size() - 1) == a) {
System.out.println(1);
} else {
System.out.println(2);
}
} else {
for (int j = 0; j < arrayList.size() - 1; j++) {
if (arrayList.get(j) > arrayList.get(j + 1)) {
count++;
break;
}
}
if (count != 0) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
| |
PASSED
|
5057307942e078d81321fe9e7a999bbf
|
train_110.jsonl
|
1621152000
|
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.
|
256 megabytes
|
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.math.BigInteger;
import java.util.Arrays;
import javax.lang.model.util.ElementScanner6;
public class Main {
static final long M = (long)1e9+7;
static final BigInteger MOD = new BigInteger(M+"");
static String[] list = new String[513];
public static void main(String[] args) {
FastInput sc = new FastInput();
int T = sc.nextInt();
while(T-->0){
int n = sc.nextInt();
int a[] = sc.readArr(n);
int ans = 0;
if(isSorted(a))
{
System.out.println("0");
continue;
}
if(a[n-1] == 1 && a[0] == n)
{
System.out.println("3");
continue;
}if(a[0] == 1 || a[n-1] == n)
{
System.out.println("1");
continue;
}else
{
System.out.println("2");
continue;
}
}
}
public static boolean match(char s[])
{
char str[] = new String("hello").toCharArray();
int index = 0;
for(int i = 0;i<s.length;i++)
{
//System.out.println(index);
if(str[index] == s[i])
{
index++;
if(index >= 5)
{
return true;
}
}
}
return false;
}
public static boolean isSorted(int a[])
{
for(int i =1;i<a.length;i++)
{
if(a[i-1] > a[i])
return false;
}
return true;
}
public static long pow(long a,long b, long M)
{
if(b == 0)
return 1;
long res = (pow(a,b/2,M)%M);
if(b%2 != 0)
return (a%M * res%M * res%M)%M;
return (res%M * res%M)%M;
}
}
class Pair implements Comparable<Pair>{
long i;
long j;
public Pair(long i,long j)
{
this.i = i;
this.j = j;
}
public Pair(){
}
public int compareTo(Pair t)
{
if(t.i>i)
return -1;
else if(t.i<i)
return 1;
//else
return 0;
}
}
class FastInput {
BufferedReader br;
StringTokenizer st;
public FastInput()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while(st == null || !st.hasMoreElements())
{
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e)
{
System.out.println(e.toString());
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public String nextLine()
{
String str = "";
try {
str = br.readLine();
} catch(IOException e)
{
System.out.println(e.toString());
}
return str;
}
public static void sort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static void sort(long[] arr)
{
ArrayList<Long> ls = new ArrayList<Long>();
for(long x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public int [] readArr(int n)
{
int [] a = new int[n];
for(int i = 0; i<n; i++)
a[i] = nextInt();
return a;
}
public void swap(int a,int b)
{
int temp = a;
a = b;
b = temp;
}
public static long gcd(long a,long b)
{
if(b == 0)
return a;
return gcd(b,a%b);
}
}
|
Java
|
["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"]
|
2 seconds
|
["1\n0\n2"]
|
NoteIn 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]$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"greedy"
] |
c212524cc1ad8e0332693e3cf644854b
|
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 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$$$.
| 900
|
For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.