結果

問題 No.59 鉄道の旅
ユーザー tenten
提出日時 2020-11-27 17:09:33
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,338 ms / 5,000 ms
コード長 970 bytes
コンパイル時間 3,542 ms
コンパイル使用メモリ 77,976 KB
実行使用メモリ 70,280 KB
最終ジャッジ日時 2024-07-23 21:38:06
合計ジャッジ時間 11,071 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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