結果

問題 No.59 鉄道の旅
ユーザー koyopro
提出日時 2015-09-30 16:21:28
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 971 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 157,820 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-12-24 21:56:14
合計ジャッジ時間 2,435 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)

int N,K,W;

template<int ME> class BIT {
public:
    int bits[1<<ME];
    int sum(int i) {
        int ret = 0;
        while(i > 0) {
            ret += bits[i];
            i -= i & -i;
        }
        return ret;
    }

    int total() {
        return sum((1<<ME)-1);
    }

    void add(int i, int add) {
        while(i < 1<<ME) {
            bits[i] += add;
            i += i & -i;
        }
    }
};

BIT<20> bt;
signed main()
{
    cin>>N>>K;
    REP(i,N) {
        cin >> W;
        int j = abs(W);
        if (W > 0) {
            if (bt.total() - bt.sum(j-1) < K) {
                bt.add(j,1);
            }
        } else {
            // あれば下ろせる
            if (bt.sum(j) - bt.sum(j-1) > 0) {
                bt.add(j,-1);
            }
        }
    }
    cout << bt.total() << endl;
    return 0;
}
0