結果

問題 No.59 鉄道の旅
ユーザー masamasa
提出日時 2016-07-29 13:19:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 904 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 71,548 KB
実行使用メモリ 7,156 KB
最終ジャッジ日時 2023-08-26 07:06:56
合計ジャッジ時間 1,554 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,904 KB
testcase_01 AC 3 ms
7,028 KB
testcase_02 AC 4 ms
6,924 KB
testcase_03 AC 4 ms
7,020 KB
testcase_04 AC 19 ms
6,984 KB
testcase_05 AC 3 ms
6,908 KB
testcase_06 AC 3 ms
6,980 KB
testcase_07 AC 4 ms
7,156 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 5 ms
6,896 KB
testcase_10 AC 6 ms
7,048 KB
testcase_11 AC 5 ms
6,892 KB
testcase_12 AC 12 ms
6,844 KB
testcase_13 AC 18 ms
7,016 KB
testcase_14 AC 18 ms
7,016 KB
testcase_15 AC 4 ms
6,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

const int W_MAX = 1000000;

class BIT
{
private:
	int n;
	vector<int> bit;
public:
	BIT();
	BIT(int a) {
		n = a;
		bit.assign(n + 1, 0);
	}
	~BIT() {
		bit.clear();
	};

	void add(int idx, int val) {
		while (idx <= n) {
			bit[idx] += val;
			idx += idx & -idx;
		}
	}

	int sum(int idx) {
		int ret = 0;
		while (idx > 0) {
			ret += bit[idx];
			idx -= idx & -idx;
		}
		return ret;
	}

	int get(int idx) {
		return sum(idx) - sum(idx - 1);
	}
};

int main() {
	int n, k, w;

	scanf("%d %d", &n, &k);

	BIT bit(W_MAX);
	for (int i = 0; i < n; i++) {
		scanf("%d", &w);
		if (w >= 0) {
			if (bit.sum(W_MAX) - bit.sum(w - 1) < k) {
				bit.add(w, 1);
			}
		} else {
			w = -w;
			if (bit.get(w) > 0) {
				bit.add(w, -1);
			}
		}
	}

	printf("%d\n", bit.sum(W_MAX));
	return 0;
}
0