Google Code Jam Notes - Moist - Java

Problem:

Moist has a hobby -- collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs to sort the cards in alphabetical order, so that he can find the cards that he wants on short notice whenever it is necessary.

The problem is -- Moist can't actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.

Moist has figured out that the robot's sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card, it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.

As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to sort his deck of cards.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each one starts with a line containing a single integer, N. The next N lines each contain the name of a figure skater, in order from the top of the deck to the bottom.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of dollars it would cost Moist to use the robot to sort his deck of trading cards.

Limits

1 ≤ T ≤ 100.
Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.
Lexicographically, the space character comes first, then come the upper case letters, then the lower case letters.

Small dataset

1 ≤ N ≤ 10.

Large dataset

1 ≤ N ≤ 100.

Sample


Input 
 

Output 
 
2
2
Oksana Baiul
Michelle Kwan
3
Elvis Stojko
Evgeni Plushenko
Kristi Yamaguchi
Case #1: 1
Case #2: 0

Analysis:
There is no need to insert the card in the right position, just calculate how many times it need to insert.
Remember the current maximum value and compare it with the new card.

Time complexity: O(n).

My solution: (Your opinion is highly appreciated)

package codeJam.google.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Zhenyi 2013 Dec 21, 2013 16:48:47 PM
 */
public class Moist {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(
				"C:/Users/Zhenyi/Downloads/C-small-practice-1.in"));
		PrintWriter out = new PrintWriter(
				"C:/Users/Zhenyi/Downloads/C-small-practice-1.out");
		// BufferedReader in = new BufferedReader(new
		// FileReader("C:/Users/Zhenyi/Downloads/C-small-practice-2.in"));
		// PrintWriter out = new
		// PrintWriter("C:/Users/Zhenyi/Downloads/C-small-practice-2.out");

		Integer T = new Integer(in.readLine());
		for (int cases = 1; cases <= T; cases++) {
			Integer N = new Integer(in.readLine());
			String[] st = new String[N];
			for (int i = 0; i < N; i++) {
				st[i] = in.readLine();
			}

			int result = 0;
			String maxBound = st[0];
			for (int i = 1; i < N; i++) {
				if (maxBound.compareTo(st[i]) > 0) {
					result++;
				} else {
					maxBound = st[i];
				}
			}

			out.write("Case #" + cases + ": " + result + "\n");
		}

		in.close();
		out.flush();
		out.close();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值