結果

問題 No.59 鉄道の旅
ユーザー hotpepsihotpepsi
提出日時 2015-07-09 23:36:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 867 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 64,672 KB
実行使用メモリ 10,980 KB
最終ジャッジ日時 2023-08-26 05:39:50
合計ジャッジ時間 1,400 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,764 KB
testcase_01 AC 5 ms
10,980 KB
testcase_02 AC 6 ms
10,820 KB
testcase_03 AC 6 ms
10,900 KB
testcase_04 AC 36 ms
10,960 KB
testcase_05 AC 6 ms
10,844 KB
testcase_06 AC 5 ms
10,772 KB
testcase_07 AC 6 ms
10,948 KB
testcase_08 AC 9 ms
10,816 KB
testcase_09 AC 9 ms
10,768 KB
testcase_10 AC 10 ms
10,788 KB
testcase_11 AC 6 ms
10,976 KB
testcase_12 AC 17 ms
10,968 KB
testcase_13 AC 35 ms
10,856 KB
testcase_14 AC 34 ms
10,820 KB
testcase_15 AC 5 ms
10,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

class BIT {
	std::vector<int> bit;
	int size;
public:
	BIT() { }
	BIT(int sz) { init(sz); }
	void init(int sz) {
		bit = std::vector<int>((size = sz) + 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 <= size) {
			bit[i] += x;
			i += i & -i;
		}
	}
};

int main(int argc, char *argv[])
{
	int N, K;
	cin >> N >> K;
	BIT bit(1000000);
	int cnt[1000001] = {};
	int tot = 0;
	for (int i = 0; i < N; ++i) {
		int W;
		cin >> W;
		if (W > 0) {
			if ((tot - bit.sum(W-1)) < K) {
				bit.add(W, 1);
				cnt[W] += 1;
				++tot;
			}
		} else {
			if (cnt[-W] > 0) {
				bit.add(-W, -1);
				cnt[-W] -= 1;
				--tot;
			}
		}
	}
	cout << bit.sum(1000000) << endl;
	return 0;
}
0