結果

問題 No.59 鉄道の旅
ユーザー mamekinmamekin
提出日時 2015-01-15 23:02:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,555 bytes
コンパイル時間 617 ms
コンパイル使用メモリ 93,448 KB
実行使用メモリ 11,824 KB
最終ジャッジ日時 2023-08-26 05:35:29
合計ジャッジ時間 1,597 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,948 KB
testcase_01 AC 3 ms
7,020 KB
testcase_02 AC 3 ms
7,120 KB
testcase_03 AC 4 ms
7,048 KB
testcase_04 AC 59 ms
8,992 KB
testcase_05 AC 3 ms
6,924 KB
testcase_06 AC 4 ms
7,052 KB
testcase_07 AC 4 ms
6,936 KB
testcase_08 AC 9 ms
7,140 KB
testcase_09 AC 8 ms
7,244 KB
testcase_10 AC 8 ms
7,152 KB
testcase_11 AC 4 ms
7,232 KB
testcase_12 AC 16 ms
6,936 KB
testcase_13 AC 45 ms
9,584 KB
testcase_14 AC 56 ms
11,824 KB
testcase_15 AC 4 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class BinaryIndexedTree
{
    int n;
    vector<int> data;
public:
    BinaryIndexedTree(int n){ // コンストラクタ
        this->n = n;
        data.assign(n+1, 0);
    }
    void add(int k, int x){ // k番目の要素にxを加算する
        ++ k;
        while(k <= n){
            data[k] += x;
            k += k & -k;
        }
    }
    int sum(int k){ // 区間[0,k]の総和を返す
        ++ k;
        int ret = 0;
        while(k > 0){
            ret += data[k];
            k -= k & -k;
        }
        return ret;
    }
    int sum(int a, int b){ // 区間[a,b]の総和を返す
        return sum(b) - sum(a-1);
    }
};

int main()
{
    int n, k;
    cin >> n >> k;

    multiset<int> ms;
    BinaryIndexedTree b(1000001);

    while(--n >= 0){
        int w;
        cin >> w;
        if(w > 0){
            if(b.sum(w, 1000000) < k){
                ms.insert(w);
                b.add(w, 1);
            }
        }
        else{
            auto it = ms.find(-w);
            if(it != ms.end()){
                ms.erase(it);
                b.add(-w, -1);
            }
        }
    }
    cout << ms.size() << endl;

    return 0;
}
0