现在的位置: 首页 > 综合 > 正文

Round A China New Grad Test 2014 Problem C. Sorting

2018年02月18日 ⁄ 综合 ⁄ 共 3162字 ⁄ 字号 评论关闭

Problem C. Sorting

Problem

Alex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their father thinks it is good to promote exchanges
if they can put their books together. Thus he has bought an one-row bookshelf for them today and put all his sons' books on it in random order. He labeled each position of the bookshelf the owner of the corresponding book ('Alex' or 'Bob').

Unfortunately, Alex and Bob went outside and didn't know what their father did. When they were back, they came to realize the problem: they usually arranged their books in their own orders, but the books
seem to be in a great mess on the bookshelf now. They have to sort them right now!!

Each book has its own worth, which is represented by an integer. Books with odd values of worth belong to Alex and the books with even values of worth belong to Bob. Alex has a habit of sorting
his books from the left to the right in an increasing order of worths, while Bob prefers to sort his books from the left to the right in a decreasing order of worths.

At the same time, they do not want to change the positions of the labels, so that after they have finished sorting the books according their rules, each book's owner's name should match with the label
in its position.

Here comes the problem. A sequence of N values s0s1, ..., sN-1 is given, which indicates the worths
of the books from the left to the right on the bookshelf currently. Please help the brothers to find out the sequence of worths after sorting such that it satisfies the above description.

Input

The first line of input contains a single integer T, the number of test cases. Each test case starts with a line containing an integer N, the number of books on the bookshelf.
The next line contains N integers separated by spaces, representing s0s1, ..., sN-1, which are the worths of the books.

Output

For each test case, output one line containing "Case #X: ", followed by t0t1, ..., tN-1 in order,
and separated by spaces. X is the test case number (starting from 1) and t0t1, ...,tN-1 forms the resulting sequence of worths of the books from the left
to the right.

Limits

1 ≤ T ≤ 30.

Small dataset

1 ≤ N ≤ 100
-100 ≤ si ≤ 100

Large dataset

1 ≤ N ≤ 1000
-1000 ≤ si ≤ 1000

Sample


Input 
 

Output 
 
2
5
5 2 4 3 1
7
-5 -12 87 2 88 20 11
Case #1: 1 4 2 3 5
Case #2: -5 88 11 20 2 -12 87
import java.util.Scanner;

public class C2014 {

	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		for(int i=1; i<=t; i++) {
			int n = in.nextInt();
			int []s = new int[n];
			int []odd = new int[n];
			int []even = new int[n];
			int oddLen = 0;
			int evenLen = 0;
			for(int j=0; j<n; j++) {
				s[j] = in.nextInt();
				if(s[j]%2 == 0) {
					even[evenLen++] = s[j];
				} else {
					odd[oddLen++] = s[j];
				}
			}
			if(oddLen > 0) {
				_quickSort(odd, 0, oddLen-1);
			}
			if(evenLen > 0) {
				_quickSort(even, 0, evenLen-1);
			}
			int oddIndex = 0;
			String res = "";
			for(int j=0; j<s.length; j++) {
				if(s[j]%2 == 0) {
					s[j] = even[--evenLen];
				} else {
					s[j] = odd[oddIndex++];
				}
				res += " " + s[j];
			}
			
			System.out.println("Case #" + i + ":" + res);
		}
	}
	static void quick(int[] str) {
		if (str.length > 0) {    //查看数组是否为空
			_quickSort(str, 0, str.length - 1);
		}
	}
	static void _quickSort(int[] list, int low, int high) {
		if (low < high) {
			int middle = getMiddle(list, low, high);  //将list数组进行一分为二
			_quickSort(list, low, middle - 1);        //对低字表进行递归排序
			_quickSort(list, middle + 1, high);       //对高字表进行递归排序
		}
	}
	static int getMiddle(int[] list, int low, int high) {
		int tmp = list[low];    //数组的第一个作为中轴
		while (low < high) {
			while (low < high && list[high] >= tmp) {
				high--;
			}
			list[low] = list[high];   //比中轴小的记录移到低端
			while (low < high && list[low] < tmp) {
				low++;
			}
			list[high] = list[low];   //比中轴大的记录移到高端
		}
		list[low] = tmp;              //中轴记录到尾
		return low;                   //返回中轴的位置
	}
}

抱歉!评论已关闭.