結果

問題 No.59 鉄道の旅
ユーザー te-shte-sh
提出日時 2016-09-02 17:10:36
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 1,016 bytes
コンパイル時間 845 ms
コンパイル使用メモリ 103,784 KB
実行使用メモリ 14,224 KB
最終ジャッジ日時 2023-09-02 21:52:50
合計ジャッジ時間 1,950 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,376 KB
testcase_01 AC 5 ms
11,568 KB
testcase_02 AC 6 ms
13,428 KB
testcase_03 AC 5 ms
12,868 KB
testcase_04 AC 43 ms
14,224 KB
testcase_05 AC 5 ms
11,556 KB
testcase_06 AC 5 ms
12,324 KB
testcase_07 AC 6 ms
11,612 KB
testcase_08 AC 10 ms
11,608 KB
testcase_09 AC 9 ms
12,808 KB
testcase_10 AC 8 ms
11,996 KB
testcase_11 AC 7 ms
12,600 KB
testcase_12 AC 26 ms
13,652 KB
testcase_13 AC 33 ms
13,680 KB
testcase_14 AC 39 ms
12,864 KB
testcase_15 AC 5 ms
11,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;

class BiTree {
  int[] buf;
  int size;

  this(int m) {
    size = m;
    buf = new int[](size + 1);
  }

  int get(int i) {
    if (!i) return 0;
    return buf[i] + get(i - (i & -i));
  }

  void add(int i, int v) {
    if (i > size) return;
    buf[i] += v;
    add(i + (i & -i), v);
  }
}

const auto maxW = 1000000;

void main()
{
  auto rd = readln.split.map!(to!int);
  auto n = rd[0], k = rd[1];
  auto wi = iota(n).map!(_ => readln.chomp.to!int);

  auto buf = new int[](maxW + 1);
  auto bitree = new BiTree(maxW);

  foreach (w; wi) {
    if (w > 0) {
      if (bitree.get(maxW) - bitree.get(w - 1) < k) {
        buf[w] += 1;
        bitree.add(w, 1);
      }
    } else {
      w = -w;
      if (buf[w] > 0) {
        buf[w] -= 1;
        bitree.add(w, -1);
      }
    }
  }

  writeln(bitree.get(maxW));
}
0