結果

問題 No.59 鉄道の旅
ユーザー atn112323atn112323
提出日時 2017-01-03 22:14:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 956 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 61,264 KB
実行使用メモリ 11,140 KB
最終ジャッジ日時 2023-08-26 08:10:38
合計ジャッジ時間 1,726 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,176 KB
testcase_01 AC 4 ms
6,960 KB
testcase_02 AC 3 ms
6,964 KB
testcase_03 AC 4 ms
7,232 KB
testcase_04 AC 41 ms
10,984 KB
testcase_05 AC 4 ms
7,016 KB
testcase_06 AC 4 ms
6,968 KB
testcase_07 AC 4 ms
7,116 KB
testcase_08 AC 10 ms
11,140 KB
testcase_09 AC 11 ms
10,708 KB
testcase_10 AC 10 ms
10,528 KB
testcase_11 AC 5 ms
6,968 KB
testcase_12 AC 16 ms
6,964 KB
testcase_13 AC 33 ms
7,412 KB
testcase_14 AC 33 ms
7,412 KB
testcase_15 AC 3 ms
7,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class BinaryIndexTree {
public:
	explicit BinaryIndexTree(int size) {
		size_ = 1;
		while (size_ < size) {
			size_ <<= 1;
		}
		tree_.resize(size_);
	}
	void add(int k, const T& x) {
		k++;
		while (k <= size_) {
			tree_[k - 1] += x;
			k += k & -k;
		}
	}
	T sum(int k) {
		k++;
		T s = 0;
		while (k > 0) {
			s += tree_[k - 1];
			k -= k & -k;
		}
		return s;
	}

private:
	int size_;
	vector<T> tree_;
};

const int MAXN = 100000;
const int MAXW = 1000000;

int N, K, W;
int picked[MAXW + 1];

int main() {
	BinaryIndexTree<int> bit(MAXW + 1);
	int ans = 0;
	cin >> N >> K;
	for (int i = 0; i < N; i++) {
		cin >> W;
		if (W > 0) {
			int cnt = bit.sum(MAXW) - bit.sum(W - 1);
			if (cnt < K) {
				ans++;
				bit.add(W, 1);
				picked[W]++;
			}
		} else if (picked[-W] > 0) {
			ans--;
			bit.add(-W, -1);
			picked[-W]--;
		}
	}
	cout << ans << endl;
	return 0;
}
0