UVA – 299 – Train Swapping


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);
		}
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: