結果
| 問題 | No.59 鉄道の旅 | 
| コンテスト | |
| ユーザー |  htensai | 
| 提出日時 | 2019-12-06 13:39:19 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 654 ms / 5,000 ms | 
| コード長 | 1,562 bytes | 
| コンパイル時間 | 2,051 ms | 
| コンパイル使用メモリ | 79,332 KB | 
| 実行使用メモリ | 65,628 KB | 
| 最終ジャッジ日時 | 2024-12-23 05:14:42 | 
| 合計ジャッジ時間 | 7,545 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 12 | 
ソースコード
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);
   }
}
            
            
            
        