Google Code Jam Notes - Sorting - Java
2013-12-27 23:58
288 查看
Problem:
Retrieved from: http://code.google.com/codejam/contest/2924486/dashboard#s=p2
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 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.
The next line contains N integers separated by spaces, representing s0, s1, ..., sN-1, which are the worths of the books.
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.
-100 ≤ si ≤ 100
-1000 ≤ si ≤ 1000
Analysis:
Sorting + mark location, time complexity O(nlog(n)).
My solution: (Your opinion is highly appreciated)
Retrieved from: http://code.google.com/codejam/contest/2924486/dashboard#s=p2
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 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.
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 s0, s1, ..., sN-1, which are the worths of the books.
Output
For 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.
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 |
Sorting + mark location, time complexity O(nlog(n)).
My solution: (Your opinion is highly appreciated)
package codeJam.google.com; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; /** * @author Zhenyi 2013 Dec 22, 2013 15:15:54 PM */ public class Sorting { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader( "C:/Users/Zhenyi/Downloads/C-large-practice.in")); FileWriter out = new FileWriter( "C:/Users/Zhenyi/Downloads/C-large-practice.out"); Integer T = new Integer(in.readLine()); for (int cases = 1; cases <= T; cases++) { Integer N = new Integer(in.readLine()); String[] st = in.readLine().split("\\s"); Integer[] a = new Integer ; boolean[] mark = new boolean ; int oddNums = 0; for (int i = 0; i < N; i++) { a[i] = new Integer(st[i]); if (a[i] % 2 != 0) { mark[i] = true; oddNums++; } } Integer[] odd = new Integer[oddNums]; Integer[] even = new Integer[N - oddNums]; int posOdd = 0; int posEven = 0; for (int i = 0; i < N; i++) { if (a[i] % 2 != 0) { odd[posOdd] = a[i]; posOdd++; } else { even[posEven] = a[i]; posEven++; } } Arrays.sort(odd); Arrays.sort(even); int locOdd = 0; int locEven = even.length - 1; for (int loc = 0; loc < a.length; loc++) { if (mark[loc]) { a[loc] = odd[locOdd]; locOdd++; } else { a[loc] = even[locEven]; locEven--; } } out.write("Case #" + cases + ":"); for (int i = 0; i < a.length; i++) { out.write(" " + a[i]); } out.write("\n"); } in.close(); out.flush(); out.close(); } }
相关文章推荐
- Google Code Jam Notes - Reverse Words - Java
- Google Code Jam Notes - Read Phone Number - Java
- Google Code Jam Notes - Rational Number Tree - Java
- Google Code Jam Notes - Good Luck - Java
- Google Code Jam Notes - Sudoku Checker - Java
- Google Code Jam Notes - Moist - Java
- Google Code Jam Notes - Minimum Scalar Product - Java
- Google Code Jam Notes - Dragon Maze - Java
- Google Code Jam Notes - File Fix-It - Java
- Google Code Jam Notes - Store Credit - Java
- Google Code Jam Notes - Cross The Maze - Java
- Google Code Jam Notes - Spaceship Defence - Java
- Google Code Jam Notes - Captain Hammer - Java
- Google Code Jam Notes - Rotate - Java
- Google Code Jam Notes - Bad Horse - Java
- Google Code Jam Notes - All Your Base - Java
- Google Code Jam Notes - Meet And Party - Java
- Google Code Jam Notes - Alien Language - Java
- Google Code Jam Notes - Rope Intranet - Java
- Google China Code Jam