結果

問題 No.59 鉄道の旅
ユーザー tentententen
提出日時 2020-11-27 17:09:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,235 ms / 5,000 ms
コード長 970 bytes
コンパイル時間 4,363 ms
コンパイル使用メモリ 79,800 KB
実行使用メモリ 71,268 KB
最終ジャッジ日時 2023-10-01 04:13:34
合計ジャッジ時間 11,286 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,780 KB
testcase_01 AC 125 ms
55,712 KB
testcase_02 AC 126 ms
55,920 KB
testcase_03 AC 133 ms
55,788 KB
testcase_04 AC 714 ms
64,740 KB
testcase_05 AC 137 ms
55,836 KB
testcase_06 AC 143 ms
56,048 KB
testcase_07 AC 135 ms
55,752 KB
testcase_08 AC 268 ms
60,304 KB
testcase_09 AC 269 ms
62,160 KB
testcase_10 AC 276 ms
60,376 KB
testcase_11 AC 235 ms
60,168 KB
testcase_12 AC 453 ms
60,464 KB
testcase_13 AC 893 ms
71,268 KB
testcase_14 AC 1,235 ms
67,120 KB
testcase_15 AC 123 ms
55,812 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();
		ArrayList<Integer> list = new ArrayList<>();
		list.add(0);
		list.add(Integer.MAX_VALUE);
		for (int i = 0; i < n; i++) {
		    int x = sc.nextInt();
		    if (x > 0) {
		        int idx = getIdx(list, x);
		        if (list.size() - idx - 1 < k) {
		            list.add(idx, x);
		        }
		    } else {
		        int idx = getIdx(list, -x);
		        if (list.get(idx) == -x) {
		            list.remove(idx);
		        }
		    }
 		}
 		System.out.println(list.size() - 2);
	}
	
	static int getIdx(ArrayList<Integer> list, int x) {
	    int left = 0;
	    int right = list.size();
	    while (right - left > 1) {
	        int m = (left + right) / 2;
	        if (list.get(m)>= x) {
	            right = m;
	        } else {
	            left = m;
	        }
	    }
	    return right;
	}
}

0