結果

問題 No.59 鉄道の旅
ユーザー noriocnorioc
提出日時 2017-08-19 14:20:54
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,267 bytes
コンパイル時間 595 ms
コンパイル使用メモリ 94,776 KB
実行使用メモリ 8,988 KB
最終ジャッジ日時 2023-09-03 16:01:44
合計ジャッジ時間 1,683 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,460 KB
testcase_01 AC 3 ms
6,484 KB
testcase_02 AC 3 ms
7,476 KB
testcase_03 AC 3 ms
7,148 KB
testcase_04 AC 25 ms
7,472 KB
testcase_05 AC 3 ms
6,468 KB
testcase_06 AC 2 ms
5,744 KB
testcase_07 AC 2 ms
6,920 KB
testcase_08 AC 6 ms
6,872 KB
testcase_09 AC 6 ms
6,936 KB
testcase_10 AC 6 ms
6,680 KB
testcase_11 AC 5 ms
7,712 KB
testcase_12 AC 21 ms
8,408 KB
testcase_13 AC 26 ms
7,516 KB
testcase_14 AC 26 ms
8,988 KB
testcase_15 AC 3 ms
6,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

int readint() {
    return readln.chomp.to!int;
}

int[] readints() {
    return readln.split.map!(to!int).array;
}

void main() {
    auto nk = readints();
    int n = nk[0], k = nk[1];

    const MAX_W = 1_000_000;
    auto bit = new BinaryIndexTree(MAX_W);
    for (int i = 0; i < n; i++) {
        auto w = readint();
        if (w > 0) { // 荷物を積む
            if (bit.sum(MAX_W) - bit.sum(w - 1) < k) { // k 個未満なら w を積む
                bit.add(w, 1);
            }
        }
        else { // 荷物を下ろす
            if (bit.sum(-w) - bit.sum(-w - 1) >= 1) { // w を下ろす
                bit.add(-w, -1);
            }
        }
    }

    int ans = bit.sum(MAX_W);
    writeln(ans);
}

class BinaryIndexTree {
    private int[] _bit;

    this(int n) {
        _bit = new int[n + 1];
    }

    int sum(int p) {
        int s = 0;
        while (p > 0) {
            s += _bit[p];
            p -= p & -p;
        }
        return s;
    }

    void add(int p, int x) {
        while (p < _bit.length) {
            _bit[p] += x;
            p += p & -p;
        }
    }
}
0