結果

問題 No.59 鉄道の旅
ユーザー assy1028assy1028
提出日時 2015-03-25 14:47:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 2,007 bytes
コンパイル時間 1,204 ms
コンパイル使用メモリ 144,028 KB
実行使用メモリ 11,556 KB
最終ジャッジ日時 2023-08-26 05:38:05
合計ジャッジ時間 2,228 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,292 KB
testcase_01 AC 4 ms
11,364 KB
testcase_02 AC 5 ms
11,344 KB
testcase_03 AC 4 ms
11,344 KB
testcase_04 AC 24 ms
11,472 KB
testcase_05 AC 5 ms
11,336 KB
testcase_06 AC 5 ms
11,556 KB
testcase_07 AC 4 ms
11,484 KB
testcase_08 AC 7 ms
11,360 KB
testcase_09 AC 7 ms
11,420 KB
testcase_10 AC 7 ms
11,468 KB
testcase_11 AC 5 ms
11,352 KB
testcase_12 AC 12 ms
11,344 KB
testcase_13 AC 20 ms
11,472 KB
testcase_14 AC 20 ms
11,392 KB
testcase_15 AC 4 ms
11,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned int uint;
typedef unsigned long long ull;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};

const int inf = 1000000009;
const double eps = 1e-8;

template<typename T, int max_n>
class BIT {
private:
    T bit[max_n + 1];
    int n;
public:
    BIT(int _n = max_n) {
	n = _n;
	memset(bit, 0, sizeof(bit));
    }

    // [1, i]
    T sum(int i) {
	T s = 0;
	while (i > 0) {
	    s += bit[i];
	    i -= i & -i;
	}
	return s;
    }
    // [a, b]
    T sum(int a, int b) {
	return sum(b) - sum(a-1);
    }

    void add(int i, T x) {
	while (i <= n) {
	    bit[i] += x;
	    i += i & -i;
	}
    }

    int lower_bound(T x) {
	int lb = 0, ub = n;
	while (ub - lb > 1) {
	    int mid = (ub + lb) / 2;
	    T v = sum(mid);
	    if (v < x) {
		lb = mid;
	    } else {
		ub = mid;
	    }
	}
	return ub;
    }

    int upper_bound(T x) {
	int lb = 1, ub = n+1;
	while (ub - lb > 1) {
	    int mid = (ub + lb) / 2;
	    T v = sum(mid);
	    if (v <= x) {
		lb = mid;
	    } else {
		ub = mid;
	    }
	}
	return ub;
    }
};

int solve(int n, int k) {
    const int buf = 1000001;
    const int maxi = buf + buf;
    BIT<int, maxi> bit;
    for (int i = 0; i < n; i++) {
	int w;
	scanf("%d", &w);
	if (w < 0) {
	    if (bit.sum(-w, -w) > 0) {
		bit.add(-w, -1);
	    }
	} else {
	    if (bit.sum(w, maxi) < k) {
		bit.add(w, 1);
	    }
	}
    }
    return bit.sum(maxi);
}

int main() {
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    */

    int n, k;
    scanf("%d%d", &n, &k);
    printf("%d\n", solve(n, k));
}
0