結果

問題 No.59 鉄道の旅
ユーザー finefine
提出日時 2017-01-13 02:12:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 766 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 164,200 KB
実行使用メモリ 7,384 KB
最終ジャッジ日時 2023-08-26 16:35:15
合計ジャッジ時間 2,690 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,280 KB
testcase_01 AC 4 ms
7,364 KB
testcase_02 AC 3 ms
7,328 KB
testcase_03 AC 3 ms
7,268 KB
testcase_04 AC 16 ms
7,288 KB
testcase_05 AC 3 ms
7,368 KB
testcase_06 AC 2 ms
7,264 KB
testcase_07 AC 2 ms
7,228 KB
testcase_08 AC 4 ms
7,336 KB
testcase_09 AC 4 ms
7,268 KB
testcase_10 AC 4 ms
7,268 KB
testcase_11 AC 4 ms
7,248 KB
testcase_12 AC 9 ms
7,344 KB
testcase_13 AC 15 ms
7,264 KB
testcase_14 AC 14 ms
7,332 KB
testcase_15 AC 3 ms
7,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int MAX_W = 1000000;

//1-indexであることに注意
struct BIT {
    int n = MAX_W, bit[MAX_W + 1] = {};
    
    int sum(int i) {
        int s = 0;
        while (i > 0) {
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }
    
    void add(int i, int x) {
        while (i <= n) {
            bit[i] += x;
            i += i & -i;
        }
    }
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K;
	cin >> N >> K;
	BIT b;
	for (int i = 0; i < N; i++) {
		int W;
		cin >> W;
		if (W > 0) {
			if (b.sum(MAX_W) - b.sum(W - 1) < K) b.add(W, 1);
		} else {
			if (b.sum(-W) > b.sum(-W - 1)) b.add(-W, -1);
		}
	}
	cout << b.sum(MAX_W) << endl;
	return 0;
}
0