結果
問題 | No.59 鉄道の旅 |
ユーザー |
![]() |
提出日時 | 2019-07-07 22:53:45 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 659 ms / 5,000 ms |
コード長 | 1,244 bytes |
コンパイル時間 | 2,013 ms |
コンパイル使用メモリ | 77,796 KB |
実行使用メモリ | 69,084 KB |
最終ジャッジ日時 | 2024-10-05 17:04:57 |
合計ジャッジ時間 | 7,436 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 12 |
ソースコード
import java.util.*;public class Main {static int N;static int[] dat;public static void main(String[] args) {Scanner sc = new Scanner(System.in);init(1000001);int n = sc.nextInt();int k = sc.nextInt();int ans = 0;for(int i = 0; i < n; i++) {int w = sc.nextInt();if(w > 0) {int t = query(w, dat.length, 0, 0, N);if(t < k) {update(w, 1);ans++;}} else {if(dat[(-1) * w + N - 1] > 0) {update((-1) * w, -1);ans--;}}}System.out.println(ans);}public static void init(int n_) {int n = 1;while(n < n_) {n *= 2;}N = n;dat = new int[2 * N - 1];}public static void update(int k, int x) {int a = k + N - 1;dat[a] += x;while(a > 0) {a = (a - 1) / 2;dat[a] = dat[2 * a + 1] + dat[2 * a + 2];}}public static int query(int a, int b, int k, int l, int r) {if((r <= a) || (l >= b)) return 0;if((a <= l) && (r <= b)) {return dat[k];} else {int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);return vl + vr;}}}