結果

問題 No.2161 Black Market
ユーザー norikamenorikame
提出日時 2022-12-13 17:27:08
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 7,000 ms
コード長 2,319 bytes
コンパイル時間 7,533 ms
コンパイル使用メモリ 336,364 KB
実行使用メモリ 32,256 KB
最終ジャッジ日時 2024-04-25 02:38:13
合計ジャッジ時間 8,214 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 82 ms
32,128 KB
testcase_21 AC 76 ms
32,128 KB
testcase_22 AC 82 ms
32,128 KB
testcase_23 AC 89 ms
32,128 KB
testcase_24 AC 94 ms
32,128 KB
testcase_25 AC 92 ms
32,000 KB
testcase_26 AC 92 ms
32,256 KB
testcase_27 AC 95 ms
32,128 KB
testcase_28 AC 91 ms
32,000 KB
testcase_29 AC 20 ms
9,984 KB
testcase_30 AC 5 ms
5,376 KB
testcase_31 AC 54 ms
18,688 KB
testcase_32 AC 88 ms
32,000 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 12 ms
7,040 KB
testcase_35 AC 17 ms
9,984 KB
testcase_36 AC 9 ms
6,784 KB
testcase_37 AC 4 ms
5,376 KB
testcase_38 AC 24 ms
10,752 KB
testcase_39 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 

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

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const int INF = (int)(1e9);

template<typename T>
struct BIT {
	int m, n;
	vector<vector<T>> d;
	BIT(int m=0, int n=0) : m(m), n(n), d(m+1, vector<T>(n+1)) {}
	void add(int i, int j_, T x=1) {
		for (i++; i<=m; i+=i&-i) for (int j=j_+1; j<=n; j+=j&-j) d[i][j] += x;
	}
	T sum(int i, int j_) {
		T x = 0;
		for (i++; i; i-=i&-i) for (int j=j_+1; j; j-=j&-j) x += d[i][j];
		return x;
	}
	T sum(int u, int l, int d, int r) {
		return sum(d-1, r-1) - sum(d-1, l-1) - sum(u-1, r-1) + sum(u-1, l-1);
	}
};

int main() {
	int n, k, l, p;
	cin >> n >> k >> l >> p;
	vector<int> a(n), b(n);
	rep(i, n) cin >> a[i] >> b[i];
	ll res = 0;
	if (n == 1) {
		if (a[0]<=l && b[0]>=p) ++res;
	}
	else {
		int ln = n / 2, rn = n - ln;
		vector<pair<ll, int>> lcosts(1<<ln);
		vector<tuple<ll, int, int>> lvals(1<<ln);
		vector<tuple<ll, ll, int>> rlst(1<<rn);
		rep(i, 1<<ln) {
			ll val = 0, cost = 0;
			int ki = __builtin_popcount(i);
			rep(j, ln) if (i&(1<<j)) {
				cost += a[j];
				val += b[j];
			}
			lcosts[i] = { cost, -1 };
			lvals[i] = { val, ki, i };
		}
		sort(all(lvals));
		vector<ll> tvals(1<<ln);
		rep(i, 1<<ln) {
			tvals[i] = get<0>(lvals[i]);
			lcosts[get<2>(lvals[i])].second = i;
		}
		sort(all(lcosts));
		rep(i, 1<<rn) {
			ll val = 0, cost = 0;
			int ki = __builtin_popcount(i);
			rep(j, rn) if (i&(1<<j)) {
				cost += a[ln+j];
				val += b[ln+j];
			}
			rlst[i] = { cost, val, ki };
		}
		sort(all(rlst));
		BIT<ll> bit(ln+1, (1<<ln)+1);
		int li = 0;
		repr(ri, 1<<rn) {
			ll rcost = get<0>(rlst[ri]), rval = get<1>(rlst[ri]), tcost = l - rcost;
			int rki = get<2>(rlst[ri]);
			while (li<(1<<ln) && lcosts[li].first<=tcost) {
				int id = lcosts[li].second, lki = get<1>(lvals[id]);
				bit.add(lki, id);
				++li;
			}
			int tid = lower_bound(all(tvals), p-rval) - tvals.begin(), tki = min(ln+1, max(0, k-rki+1));
			res += bit.sum(0, tid, tki, (1<<ln));
		}
	}
	cout << res << endl;
	return 0;
}
0