結果

問題 No.59 鉄道の旅
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-07 22:53:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 682 ms / 5,000 ms
コード長 1,244 bytes
コンパイル時間 2,457 ms
コンパイル使用メモリ 77,024 KB
実行使用メモリ 59,856 KB
最終ジャッジ日時 2024-04-15 16:39:47
合計ジャッジ時間 7,880 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
49,776 KB
testcase_01 AC 143 ms
49,588 KB
testcase_02 AC 141 ms
49,416 KB
testcase_03 AC 147 ms
49,660 KB
testcase_04 AC 682 ms
58,536 KB
testcase_05 AC 152 ms
49,840 KB
testcase_06 AC 159 ms
49,876 KB
testcase_07 AC 150 ms
49,448 KB
testcase_08 AC 298 ms
55,780 KB
testcase_09 AC 293 ms
55,584 KB
testcase_10 AC 294 ms
55,720 KB
testcase_11 AC 267 ms
54,856 KB
testcase_12 AC 584 ms
58,760 KB
testcase_13 AC 667 ms
58,944 KB
testcase_14 AC 613 ms
59,856 KB
testcase_15 AC 146 ms
49,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }
  }
}
0