結果

問題 No.59 鉄道の旅
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-05 01:18:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,443 bytes
コンパイル時間 1,536 ms
コンパイル使用メモリ 59,536 KB
実行使用メモリ 7,432 KB
最終ジャッジ日時 2023-08-26 07:03:39
合計ジャッジ時間 1,512 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,848 KB
testcase_01 AC 4 ms
6,912 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 4 ms
6,840 KB
testcase_04 AC 17 ms
7,200 KB
testcase_05 AC 4 ms
6,932 KB
testcase_06 AC 5 ms
6,956 KB
testcase_07 AC 4 ms
6,868 KB
testcase_08 AC 6 ms
7,020 KB
testcase_09 AC 5 ms
6,868 KB
testcase_10 AC 6 ms
6,860 KB
testcase_11 AC 5 ms
6,908 KB
testcase_12 AC 11 ms
7,380 KB
testcase_13 AC 17 ms
7,276 KB
testcase_14 AC 16 ms
7,432 KB
testcase_15 AC 5 ms
6,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
/*
 * Fenwick Treeの実装.
 * 区間[1, n]において, いわゆるfoldlの操作を高速化することができる.
 */
template<typename T>
class FenwickTree {
public:
    vector<T> elt_;
    int n_;

    FenwickTree() { }

    FenwickTree(int n) {
        elt_ = vector<T>(n + 1);
        n_ = n;
        /* 初期化処理 */
        for (int i = 0; i <= n_; i++) {
            elt_[i] = 0;
        } 
    }

    void add(int i, T x) {
        while (i <= elt_.size()) {
            elt_[i] += x;
            i += i & -i;
        }
    }

    T query(int i) {
        T s = 0;
        while (i > 0) {
            /* クエリの処理 */
            s += elt_[i];
            i -= i & -i;
        }
        return s;
    }
};

const int kMAX_N = 100010;
const int kMAX_W = 1000010;

int N, K;
int W[kMAX_N];

void Solve() {
    FenwickTree<int> tree = FenwickTree<int>(kMAX_W);

    for (int i = 0; i < N; i++) {
        if (W[i] > 0 && tree.query(kMAX_W) - tree.query(W[i] - 1) < K) {
            tree.add(W[i], 1);
        } else if (W[i] < 0 && tree.query(abs(W[i])) - tree.query(abs(W[i]) - 1) > 0) {
            tree.add(abs(W[i]), -1);
        }
    }
    cout << tree.query(kMAX_W) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

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

    Solve();

    return 0;
}
0