結果

問題 No.59 鉄道の旅
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-08 23:14:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,034 bytes
コンパイル時間 1,207 ms
コンパイル使用メモリ 166,492 KB
実行使用メモリ 11,448 KB
最終ジャッジ日時 2023-09-17 03:29:03
合計ジャッジ時間 2,071 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,864 KB
testcase_01 AC 5 ms
10,812 KB
testcase_02 AC 4 ms
11,076 KB
testcase_03 AC 4 ms
10,812 KB
testcase_04 AC 30 ms
11,004 KB
testcase_05 AC 3 ms
10,776 KB
testcase_06 AC 4 ms
10,744 KB
testcase_07 AC 4 ms
10,744 KB
testcase_08 AC 8 ms
11,112 KB
testcase_09 AC 7 ms
11,052 KB
testcase_10 AC 7 ms
10,816 KB
testcase_11 AC 6 ms
10,740 KB
testcase_12 AC 16 ms
11,340 KB
testcase_13 AC 32 ms
11,356 KB
testcase_14 AC 32 ms
11,448 KB
testcase_15 AC 4 ms
10,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct BinaryIndexedTree {
    vector<T> data;
    int n;
    BinaryIndexedTree(int n_) : n(n_ + 1), data(n_ + 1, 0) {}
    T sum(int i) {
        T ret(0);
        for (int x = i; x > 0; x -= x & -x)
            ret += data[x];
        return ret;
    }
    void add(int i, T a) {
        for (int x = i; x <= n; x += x & -x)
            data[x] += a;
    }
};

int main() {
    int N, K, W[100010];
    cin >> N >> K;
    for (int i = 0; i < N; i++) {
        cin >> W[i];
    }

    int cnt[1000020] = {};
    BinaryIndexedTree<int> bit(1000020);
    int MAX = 1000010;

    for (int i = 0; i < N; i++) {
        if (W[i] > 0) {
            if (bit.sum(MAX) - bit.sum(W[i] - 1) < K) {
                bit.add(W[i], 1);
                cnt[W[i]]++;
            }
        } else {
            W[i] *= -1;
            if (cnt[W[i]]) {
                cnt[W[i]]--;
                bit.add(W[i], -1);
            }
        }
    }
    cout << bit.sum(MAX) << endl;
}
0