import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder("");
String m = "";
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++) {
int num=Integer.parseInt(br.readLine());
String[] str=br.readLine().split(" ");
int[] arr=new int[num];
for(int j=0;j<num;j++){
arr[j]=Integer.parseInt(str[j]);
}
sb.append("Optimal train swapping takes ").append(numofSwap(arr)).append(" swaps.\n");
}
System.out.print(sb);
}
static int numofSwap(int []arr){
int counter=0;
for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
counter++;
}
}
}
return counter;
}
}
No comments:
Post a Comment