結果

問題 No.59 鉄道の旅
ユーザー htensaihtensai
提出日時 2019-12-06 13:39:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 676 ms / 5,000 ms
コード長 1,562 bytes
コンパイル時間 4,829 ms
コンパイル使用メモリ 75,724 KB
実行使用メモリ 68,128 KB
最終ジャッジ日時 2023-08-24 19:51:42
合計ジャッジ時間 9,553 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,692 KB
testcase_01 AC 120 ms
55,944 KB
testcase_02 AC 120 ms
56,124 KB
testcase_03 AC 128 ms
55,840 KB
testcase_04 AC 633 ms
64,692 KB
testcase_05 AC 130 ms
55,724 KB
testcase_06 AC 136 ms
57,512 KB
testcase_07 AC 128 ms
56,484 KB
testcase_08 AC 281 ms
59,724 KB
testcase_09 AC 276 ms
60,748 KB
testcase_10 AC 292 ms
60,516 KB
testcase_11 AC 229 ms
59,576 KB
testcase_12 AC 464 ms
60,564 KB
testcase_13 AC 676 ms
64,040 KB
testcase_14 AC 615 ms
68,128 KB
testcase_15 AC 118 ms
56,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		TreeMap<Integer, Integer> map = new TreeMap<>();
		int count = 0;
		int max = 0;
		int maxCount = 0;
		for (int i = 0; i < n; i++) {
		    int x = sc.nextInt();
		    if (x > 0) {
		        if (count >= k && max >= x) {
		            continue;
		        }
		        if (map.containsKey(x)) {
		            map.put(x, map.get(x) + 1);
		        } else {
		            map.put(x, 1);
		        }
		        if (count < k) {
		            count++;
		            if (count == k) {
		                max = map.firstKey();
		                maxCount = 0;
		            }
		        } else {
		            count++;
	                maxCount++;
	                if (map.get(max) <= maxCount) {
	                    max = map.higherKey(max);
	                    maxCount = 0;
	                }
		        }
		    } else {
		        x *= -1;
		        if (map.containsKey(x)) {
		            int cnt = map.get(x);
		            if (cnt == 1) {
		                map.remove(x);
		            } else {
		                map.put(x, cnt - 1);
		            }
		            count--;
		        } else {
		            continue;
		        }
		        if (count >= k && max <= x) {
		            maxCount--;
		            if (maxCount < 0) {
		                max = map.lowerKey(max);
		                maxCount = map.get(max) - 1;
		            }
		        }
		    }
		}
		System.out.println(count);
   }
}

0