結果
| 問題 |
No.59 鉄道の旅
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-11-22 21:01:59 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,567 bytes |
| コンパイル時間 | 614 ms |
| コンパイル使用メモリ | 78,340 KB |
| 実行使用メモリ | 7,536 KB |
| 最終ジャッジ日時 | 2024-12-25 00:19:02 |
| 合計ジャッジ時間 | 1,757 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 9 RE * 3 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using namespace std;
template <typename T>
struct binary_indexed_tree { // on monoid
vector<T> data;
T unit;
function<T (T,T)> append; // associative
template <typename F>
binary_indexed_tree(size_t n, T a_unit, F a_append) : data(n, a_unit), unit(a_unit), append(a_append) {}
void point_append(size_t i, T w) { // data[i] += w
for (size_t j = i+1; j <= data.size(); j += j & -j) data[j-1] = append(data[j-1], w);
}
int initial_range_concat(size_t i) { // sum [0, i)
T acc = unit;
for (size_t j = i; 0 < j; j -= j & -j) acc = append(acc, data[j-1]);
return acc;
}
T point_get(size_t i) {
return initial_range_concat(i+1) - initial_range_concat(i);
}
};
int main() {
int n, k; cin >> n >> k;
vector<int> ws(n); repeat (i,n) cin >> ws[i];
binary_indexed_tree<int> bit(*whole(max_element, ws) + 1, int(), plus<int>());
for (int w : ws) {
int i = bit.data.size() - abs(w);
if (w > 0) {
if (bit.initial_range_concat(i+1) < k) {
bit.point_append(i, 1);
}
} else if (w < 0) {
if (bit.point_get(i)) {
bit.point_append(i, -1);
}
}
}
cout << bit.initial_range_concat(bit.data.size()) << endl;
return 0;
}