結果

問題 No.59 鉄道の旅
ユーザー not_522not_522
提出日時 2015-08-17 16:40:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 5,000 ms
コード長 743 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 145,436 KB
実行使用メモリ 7,072 KB
最終ジャッジ日時 2023-08-26 06:35:18
合計ジャッジ時間 2,354 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,936 KB
testcase_01 AC 4 ms
7,000 KB
testcase_02 AC 4 ms
6,888 KB
testcase_03 AC 4 ms
7,072 KB
testcase_04 AC 39 ms
6,908 KB
testcase_05 AC 4 ms
7,004 KB
testcase_06 AC 4 ms
6,936 KB
testcase_07 AC 4 ms
6,880 KB
testcase_08 AC 7 ms
6,936 KB
testcase_09 AC 8 ms
7,016 KB
testcase_10 AC 7 ms
6,880 KB
testcase_11 AC 6 ms
6,972 KB
testcase_12 AC 17 ms
6,936 KB
testcase_13 AC 37 ms
6,856 KB
testcase_14 AC 34 ms
7,064 KB
testcase_15 AC 4 ms
7,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class BIT {
private:
  vector<int> v;

public:
  BIT(int n) : v(vector<int>(n + 1, 0)) {}

  // [0, i)
  int sum(int i) {
    return i ? v[i] + sum(i & (i - 1)) : 0;
  }

  // [a, b)
  int sum(int a, int b) {
    return sum(b) - sum(a);
  }

  void add(int i, int x) {
    for (++i; i < (int)v.size(); i += i & -i) v[i] += x;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  BIT bit(1000001);
  int res = 0;
  for (int i = 0; i < n; ++i) {
    int w;
    cin >> w;
    if (w > 0) {
      if (bit.sum(w, 1000001) >= k) continue;
      bit.add(w, 1);
      ++res;
    } else {
      if (bit.sum(-w, -w + 1) == 0) continue;
      bit.add(-w, -1);
      --res;
    }
  }
  cout << res << endl;
}
0