結果

問題 No.1285 ゴミ捨て
ユーザー ks2mks2m
提出日時 2020-11-13 21:25:48
言語 Java19
(openjdk 21)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 2,086 bytes
コンパイル時間 5,143 ms
コンパイル使用メモリ 75,876 KB
実行使用メモリ 58,568 KB
最終ジャッジ日時 2023-08-19 07:11:20
合計ジャッジ時間 8,552 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,344 KB
testcase_01 AC 40 ms
49,968 KB
testcase_02 AC 42 ms
49,364 KB
testcase_03 AC 39 ms
49,520 KB
testcase_04 AC 38 ms
49,340 KB
testcase_05 AC 39 ms
49,448 KB
testcase_06 AC 40 ms
49,472 KB
testcase_07 AC 262 ms
58,568 KB
testcase_08 AC 97 ms
53,132 KB
testcase_09 AC 183 ms
56,916 KB
testcase_10 AC 236 ms
57,424 KB
testcase_11 AC 226 ms
57,556 KB
testcase_12 AC 196 ms
57,016 KB
testcase_13 AC 189 ms
55,352 KB
testcase_14 AC 230 ms
57,784 KB
testcase_15 AC 200 ms
57,052 KB
testcase_16 AC 132 ms
56,676 KB
testcase_17 AC 136 ms
55,804 KB
testcase_18 AC 134 ms
56,020 KB
testcase_19 AC 173 ms
56,856 KB
testcase_20 AC 128 ms
55,056 KB
testcase_21 AC 153 ms
54,424 KB
testcase_22 AC 240 ms
56,688 KB
testcase_23 AC 246 ms
57,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(br.readLine());
		}
		br.close();

		Arrays.sort(a);

		MultiTreeSet<Integer> set = new MultiTreeSet<>();
		set.add(a[0]);
		for (int i = 1; i < n; i++) {
			Integer v = set.lower(a[i] - 1);
			if (v != null) {
				set.remove(v);
			}
			set.add(a[i]);
		}
		System.out.println(set.sizeAll());
	}

	static class MultiTreeSet<T> {
		TreeMap<T, Integer> map = new TreeMap<>();
		int size = 0;

		void add(T e) {
			map.put(e, map.getOrDefault(e, 0) + 1);
			size++;
		}

		void remove(T e) {
			if (e != null && map.containsKey(e)) {
				int val = map.get(e);
				if (val == 1) {
					map.remove(e);
				} else {
					map.put(e, val - 1);
				}
				size--;
			}
		}

		int sizeAll() {
			return size;
		}

		int sizeUnq() {
			return map.size();
		}

		boolean contains(T e) {
			return map.containsKey(e);
		}

		boolean isEmpty() {
			return size == 0;
		}

		T lower(T e) {
			return map.lowerKey(e);
		}

		T floor(T e) {
			return map.floorKey(e);
		}

		T higher(T e) {
			return map.higherKey(e);
		}

		T ceiling(T e) {
			return map.ceilingKey(e);
		}

		T first() {
			return map.firstKey();
		}

		T last() {
			return map.lastKey();
		}

		T pollFirst() {
			T e = map.firstKey();
			remove(e);
			return e;
		}

		T pollLast() {
			T e = map.lastKey();
			remove(e);
			return e;
		}

		@SuppressWarnings("unchecked")
		T[] toArrayUnq() {
			Object[] arr = map.keySet().toArray();
			return (T[]) arr;
		}

		@SuppressWarnings("unchecked")
		T[] toArrayAll() {
			Object[] arr = new Object[size];
			int idx = 0;
			for (T e : map.keySet()) {
				int num = map.get(e);
				for (int i = 0; i < num; i++) {
					arr[idx] = e;
					idx++;
				}
			}
			return (T[]) arr;
		}
	}
}
0