Round A China New Grad Test 2014 Problem C. Sorting

Source: Internet
Author: User

Problem C. sortingProblemAlex 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 positi On 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 shoshould match with the label in its position. here comes the problem. A sequence of N values s0, s1 ,..., 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. inputThe 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 s0, s1 ,..., sN-1, which are the worths of the books. outputFor each test case, output one line containing "Case # X:", followed by t0, t1 ,..., tN-1 in order, and separated by spaces. X is the test case number (starting from 1) and t0, t1 ,..., tN-1 forms the resulting sequence of worths of the books from the left to the right. limits1 ≤ T ≤ 30. small dataset1 ≤ N ≤ 100-100 ≤ si ≤ 100 Large dataset1 ≤ N ≤ 1000-1000 ≤ si ≤ 1000 Sample Input Output 255 2 4 3 17-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. break;

Public class C2014 {public static void main (String [] args) {role in = new role (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) {// check whether the array is empty _ quickSort (str, 0, str. length-1) ;}} static void _ quickSort (int [] list, int low, int high) {if (low 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.