結果

問題 No.59 鉄道の旅
ユーザー pekempeypekempey
提出日時 2015-08-19 16:10:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 966 bytes
コンパイル時間 1,484 ms
コンパイル使用メモリ 145,672 KB
実行使用メモリ 11,236 KB
最終ジャッジ日時 2023-08-26 06:34:37
合計ジャッジ時間 2,283 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,996 KB
testcase_01 AC 4 ms
11,068 KB
testcase_02 AC 5 ms
11,236 KB
testcase_03 AC 4 ms
10,900 KB
testcase_04 AC 40 ms
10,908 KB
testcase_05 AC 5 ms
11,012 KB
testcase_06 AC 5 ms
10,908 KB
testcase_07 AC 5 ms
10,872 KB
testcase_08 AC 8 ms
11,136 KB
testcase_09 AC 8 ms
11,184 KB
testcase_10 AC 8 ms
11,104 KB
testcase_11 AC 7 ms
10,704 KB
testcase_12 AC 19 ms
10,824 KB
testcase_13 AC 38 ms
10,944 KB
testcase_14 AC 36 ms
11,064 KB
testcase_15 AC 5 ms
10,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

struct BIT {
	int size;
	vector<ll> bit;
	BIT(int size) : size(size), bit(size + 1) {}
	void add(int k, ll x) {
		while (k <= size) {
			bit[k] += x;
			k += k & -k;
		}
	}
	ll sum(int k) {
		ll res = 0;
		while (k > 0) {
			res += bit[k];
			k -= k & -k;
		}
		return res;
	}
};

int main() {
	int N, K;
	cin >> N >> K;
	BIT bit(1010101);

	rep (i, N) {
		int W;
		cin >> W;

		if (W > 0) {
			int total = bit.sum(1010000) - bit.sum(W - 1);
			if (total < K) {
				bit.add(W, 1);	
			}
		} else {
			W *= -1;
			if (bit.sum(W) - bit.sum(W - 1) > 0) {
				bit.add(W, -1);
			}
		}
	}

	int ans = bit.sum(1010000);
	cout << ans << endl;
}
0