- ID: UVA – 299 – Train Swapping
- Difficulty : Easy
- Type : Adhoc
- Description :
- 2 nested loops and count swapps, straight forward
- Code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long T = in.nextLong();
while (T-- != 0) {
long swaps = 0;
int[] a = new int[in.nextInt()];
for (int i = 0; i < a.length; i++)
a[i] = in.nextInt();
for (int i = 0; i < a.length - 1; i++)
for (int j = i + 1; j < a.length; j++)
if (a[i] > a[j])
swaps++;
System.out.format("Optimal train swapping takes %d swaps.\n",swaps);
}
}
}