結果
問題 | No.59 鉄道の旅 |
ユーザー |
|
提出日時 | 2019-03-07 14:04:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 21 ms / 5,000 ms |
コード長 | 1,194 bytes |
コンパイル時間 | 635 ms |
コンパイル使用メモリ | 69,780 KB |
実行使用メモリ | 7,680 KB |
最終ジャッジ日時 | 2024-06-23 14:49:52 |
合計ジャッジ時間 | 1,721 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 12 |
ソースコード
#include <iostream> #include <vector> using namespace std; using i64 = int64_t; template <class T> struct BIT { int N; vector<T> data; explicit BIT(int n); T sum(int k); // k 番目以下の総和 void add(int k, T val); }; // {{{ † BIT 実装 template <class T> BIT<T>::BIT(int n) { N = n + 1; data.assign(N, 0); } template <class T> T BIT<T>::sum(int k) { T res = 0; for(++k; k>0; k-=k&-k) { res += data[k]; } return res; } template <class T> void BIT<T>::add(int k, T x) { for(++k; k<N; k+=k&-k) { data[k] += x; } } // }}} int f(int n, int k, vector<int> &w) { constexpr int MAXI = 1'000'005; BIT<int> tree(MAXI+1); int cnt; for(int i=0; i<n; ++i) { if(w[i] > 0) { // w[i] 以上の個数を数える cnt = tree.sum(MAXI) - tree.sum(w[i]-1); if(cnt < k) { tree.add(w[i], 1); } } else { w[i] = -w[i]; cnt = tree.sum(w[i]) - tree.sum(w[i]-1); if(cnt) { tree.add(w[i], -1); } } } int res = tree.sum(MAXI); return res; } int main(void) { int N, K; scanf("%d%d", &N, &K); vector<int> W(N); for(int i=0; i<N; ++i) { scanf("%d", &W[i]); } int res = f(N, K, W); printf("%d\n", res); return 0; }