結果

問題 No.59 鉄道の旅
ユーザー koyoprokoyopro
提出日時 2015-09-30 16:21:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 971 bytes
コンパイル時間 1,102 ms
コンパイル使用メモリ 144,384 KB
実行使用メモリ 7,484 KB
最終ジャッジ日時 2023-08-26 06:44:39
合計ジャッジ時間 2,230 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 36 ms
7,296 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 8 ms
7,484 KB
testcase_09 AC 8 ms
7,208 KB
testcase_10 AC 8 ms
6,872 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 33 ms
4,380 KB
testcase_14 AC 31 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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