AtCoder Beginner Contest 001

Submission #673894

Source codeソースコード

import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

public class Main {
	class Interval implements Comparable{
		int start_h;
		int start_m;
		int end_h;
		int end_m;	

		public Interval(int start_h, int start_m, int end_h, int end_m) {
			this.start_h = start_h;
			this.start_m = start_m;
			this.end_h = end_h;
			this.end_m = end_m;
		}

		public int compareTo(Object obj) {
			if (this.start_h < ((Interval)obj).start_h) return -1;
			else if (this.start_h == ((Interval)obj).start_h) {
				if (this.start_m < ((Interval)obj).start_m) return -1;
				else if (this.start_m == ((Interval)obj).start_m) {
					if (this.end_h < ((Interval)obj).end_h) return -1;
					else if (this.end_h == ((Interval)obj).end_h) {
						if (this.end_m < ((Interval)obj).end_m) return -1;
						else if (this.end_m == ((Interval)obj).end_m) return 0;
						else return 1;
					}
					else return 1;
				}
				else return 1;
			}
			else return 1;
		}
		
		public Interval isBetweenInterval(Interval interval) throws Exception {
			if (interval.start_h < this.end_h || (interval.start_h == this.end_h && interval.start_m <= this.end_m)) {
				if (interval.end_h < this.end_h || (interval.end_h == this.end_h && interval.end_m <= this.end_m)) {
					return this;
				} else if (this.end_h < interval.end_h || (this.end_h == interval.end_h && this.end_m < interval.end_m)) {
					return new Interval(this.start_h, this.start_m, interval.end_h, interval.end_m);
				} else {
					throw new Exception();
				}
			} else {
				return null;
			}
		}

		public void round() {
			this.start_m = this.start_m / 5 * 5;
			int end_m = this.end_m % 5 == 0 ? this.end_m / 5 : this.end_m / 5 + 1;
			this.end_m = end_m * 5;
			if (this.end_m == 60) {
				this.end_h = this.end_h + 1;	
				this.end_m = 0;
			}
		}

		public String toString() {
			StringBuilder str = new StringBuilder();
			str.append(String.format("%02d", this.start_h));	
			str.append(String.format("%02d", this.start_m));	
			str.append("-");
			str.append(String.format("%02d", this.end_h));	
			str.append(String.format("%02d", this.end_m));	
			return str.toString();
		}
	}
	
	Interval intervals[];
	ArrayList<String> answers = new ArrayList<String>();

	public static void main(String args[]) {
		new Main().solve();
	}

	void solve() {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		String start[] = new String[n];
		String end[] = new String[n];
		intervals = new Interval[n];
		for (int i = 0; i < n; i++) {
			String time = scan.next();
			String t[] = time.split("-");
			start[i] = t[0];
			end[i] = t[1];
			intervals[i] = new Interval(Integer.parseInt(t[0].substring(0, 2)), Integer.parseInt(t[0].substring(2, 4)), 
										Integer.parseInt(t[1].substring(0, 2)), Integer.parseInt(t[1].substring(2, 4)));
		}

		for (Interval interval : intervals) {
			interval.round();
		}

		Arrays.sort(intervals);
		//for (Interval interval : intervals) {
		//	System.out.printf("%d:%d - %d:%d\n", interval.start_h, interval.start_m, interval.end_h, interval.end_m);
		//}

		try {
			integrate(intervals[0], 0);
		} catch(Exception ex) {ex.printStackTrace();}

		for (String answer : answers) {
			System.out.println(answer);
		}

	}

	void integrate(Interval interval, int index) throws Exception{
		int i = index + 1;
		if (i == intervals.length) answers.add(interval.toString());
		else {
			Interval inter = interval.isBetweenInterval(intervals[i]);	
			if (inter != null) {
				// System.out.println(i + ": new Interval = " + inter);
				integrate(inter, i);
			} else {
				// System.out.println(i + ": answer interval = " + interval);
				answers.add(interval.toString());
				integrate(intervals[i], i);
			}
		}
	}


}

Submission

Task問題 D - 感雨時刻の整理
User nameユーザ名 kawaryu
Created time投稿日時
Language言語 Java (OpenJDK 1.7.0)
Status状態 AC
Score得点 100
Source lengthソースコード長 3852 Byte
File nameファイル名
Exec time実行時間 1056 ms
Memory usageメモリ使用量 48428 KB

Test case

Set

Set name Score得点 / Max score Cases
all 100 / 100 00_sample_01.txt,00_sample_02.txt,00_sample_03.txt,cho_cho_chokudai.txt,chokudai_ga_cho.txt,test_01.txt,test_02.txt,test_03.txt,test_04.txt,test_05.txt,test_06.txt,test_07.txt,test_08.txt,test_09.txt,test_10.txt,test_11.txt,test_12.txt,test_13.txt,test_14.txt,test_15.txt,test_16.txt,test_17.txt,test_18.txt,test_19.txt,test_20.txt,test_21.txt,test_22.txt,test_23.txt,test_24.txt,test_25.txt,test_26.txt,test_27.txt,test_28.txt,test_29.txt,test_30.txt,test_31.txt,test_32.txt,test_33.txt,test_34.txt,test_35.txt,test_36.txt,test_37.txt,test_38.txt,test_39.txt,test_40.txt,test_41.txt,test_42.txt,test_43.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
00_sample_01.txt AC 359 ms 23632 KB
00_sample_02.txt AC 360 ms 23580 KB
00_sample_03.txt AC 359 ms 23532 KB
cho_cho_chokudai.txt AC 882 ms 45740 KB
chokudai_ga_cho.txt AC 943 ms 47352 KB
test_01.txt AC 432 ms 24528 KB
test_02.txt AC 433 ms 24460 KB
test_03.txt AC 436 ms 24472 KB
test_04.txt AC 433 ms 24520 KB
test_05.txt AC 437 ms 24484 KB
test_06.txt AC 413 ms 24048 KB
test_07.txt AC 389 ms 23804 KB
test_08.txt AC 388 ms 23596 KB
test_09.txt AC 436 ms 25332 KB
test_10.txt AC 453 ms 25784 KB
test_11.txt AC 436 ms 24704 KB
test_12.txt AC 423 ms 24132 KB
test_13.txt AC 381 ms 23856 KB
test_14.txt AC 428 ms 24880 KB
test_15.txt AC 439 ms 25552 KB
test_16.txt AC 383 ms 23876 KB
test_17.txt AC 418 ms 24948 KB
test_18.txt AC 380 ms 23832 KB
test_19.txt AC 404 ms 24176 KB
test_20.txt AC 414 ms 24860 KB
test_21.txt AC 1041 ms 47476 KB
test_22.txt AC 1056 ms 46736 KB
test_23.txt AC 1055 ms 47916 KB
test_24.txt AC 1027 ms 47520 KB
test_25.txt AC 1018 ms 47420 KB
test_26.txt AC 971 ms 48428 KB
test_27.txt AC 933 ms 47552 KB
test_28.txt AC 991 ms 47740 KB
test_29.txt AC 362 ms 23572 KB
test_30.txt AC 375 ms 23672 KB
test_31.txt AC 627 ms 32440 KB
test_32.txt AC 366 ms 23576 KB
test_33.txt AC 998 ms 44920 KB
test_34.txt AC 365 ms 23556 KB
test_35.txt AC 386 ms 24052 KB
test_36.txt AC 985 ms 47604 KB
test_37.txt AC 993 ms 47972 KB
test_38.txt AC 976 ms 47744 KB
test_39.txt AC 1005 ms 47756 KB
test_40.txt AC 1042 ms 47096 KB
test_41.txt AC 923 ms 45692 KB
test_42.txt AC 1023 ms 47472 KB
test_43.txt AC 945 ms 45764 KB