1028.国勢調査

2512 ワード

1028.国勢調査(20)
時間の制限
200 ms
メモリ制限
32000 kB
コード長制限
8000 B
採点プログラム
Standard
作者
CHEN,Yue
ある町で国勢調査を行い、住民全員の誕生日を得た。町の一番年上と一番若い人を見つけて、プログラムを書いてください。
ここでは、入力された日付はすべて合法的ですが、必ずしも合理的ではありません。町で200歳以上のお年寄りがいないと仮定して、今日は2014年9月6日です。だから、200歳以上の誕生日と未誕生誕生日は全部合理的ではないです。濾過されるべきです。
入力フォーマット:
最初の行に正の整数Nを入力し、値を(0、105)にします。その後、N行に1人の名前(5文字を超えない文字列)と、「yyy/mm/dd」(年/月/日)の形式で与えられた誕生日です。一番年上と若い人が並んでいないことを保証します。
出力フォーマット:
有効な誕生日の個数、最年長者、最若者の名前を1行に順次出力します。その間はスペースで区切ります。
入力サンプル:
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
出力例:
3 Tom John
             Javaを使うとテストポイントがタイムアウトします。同じアルゴリズムをc++に置き換えればいいです。JavaのIO処理が遅すぎます。コードは以下の通りです。
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Twentyeight {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));

        int N = Integer.parseInt(scanner.nextLine());
        int amount = 0, max = 0, min = 20140907;
        String maxname = "", minname = "";

        for (int i = 0; i < N; i++) {
            String str = scanner.nextLine();

            int count = 0;
            for (int j = 0; j < str.length(); j++) {
                if (str.charAt(j) == ' ') {
                    count = j;
                }
            }
            int date = Integer.parseInt(str.substring(count + 1, count + 5) + str.substring(count + 6, count + 8)
                    + str.substring(count + 9, count + 11));
            if (date >= 18140906 && date <= 20140906) {
                amount++;
                if (date > max) {
                    max = date;
                    maxname = String.valueOf(str.substring(0, count));
                }
                if (date < min) {
                    min = date;
                    minname = String.valueOf(str.substring(0, count));
                }
            }
        }
        if (amount == 0) {
            System.out.println(0);

        } else {
            System.out.println(amount + " " + minname + " " + maxname);
        }
        scanner.close();
    }
}