結果
問題 | No.59 鉄道の旅 |
ユーザー | hotpepsi |
提出日時 | 2015-07-09 23:36:14 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 46 ms / 5,000 ms |
コード長 | 867 bytes |
コンパイル時間 | 647 ms |
コンパイル使用メモリ | 63,636 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-12-24 20:38:48 |
合計ジャッジ時間 | 1,575 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 12 |
ソースコード
#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; }