結果

問題 No.2161 Black Market
ユーザー ぷらぷら
提出日時 2022-12-12 00:19:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 310 ms / 7,000 ms
コード長 2,771 bytes
コンパイル時間 2,840 ms
コンパイル使用メモリ 216,880 KB
実行使用メモリ 8,048 KB
最終ジャッジ日時 2024-04-23 18:34:54
合計ジャッジ時間 5,172 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 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 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 143 ms
8,040 KB
testcase_21 AC 145 ms
8,044 KB
testcase_22 AC 30 ms
5,376 KB
testcase_23 AC 195 ms
8,048 KB
testcase_24 AC 310 ms
7,960 KB
testcase_25 AC 74 ms
7,532 KB
testcase_26 AC 242 ms
8,044 KB
testcase_27 AC 25 ms
5,888 KB
testcase_28 AC 29 ms
5,888 KB
testcase_29 AC 10 ms
5,376 KB
testcase_30 AC 11 ms
5,376 KB
testcase_31 AC 159 ms
7,132 KB
testcase_32 AC 25 ms
6,016 KB
testcase_33 AC 25 ms
5,376 KB
testcase_34 AC 5 ms
5,376 KB
testcase_35 AC 6 ms
5,376 KB
testcase_36 AC 4 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 8 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    BinaryIndexedTree(){}
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,T y) {
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int x) {
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    inline T sum(int l, int r) {//[l,r]
        return sum(r)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
};

int main() {
    int N,K,L,P;
    cin >> N >> K >> L >> P;
    vector<int>A(N),B(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i] >> B[i];
    }
    int a = N/2,b = N-a;
    vector<vector<pair<int,int>>>tmp1(a+1),tmp2(b+1);
    for(int i = 0; i < (1 << a); i++) {
        long long sum1 = 0,sum2 = 0;
        for(int j = 0; j < a; j++) {
            if(1 & (i >> j)) {
                sum1 += A[j];
                sum2 += B[j];
            }
        }
        if(sum1 <= L) {
            if(sum2 >= P) sum2 = P;
            tmp1[__builtin_popcount(i)].push_back({sum1,sum2});
        }
    }
    for(int i = 0; i < (1 << b); i++) {
        long long sum1 = 0,sum2 = 0;
        for(int j = 0; j < b; j++) {
            if(1 & (i >> j)) {
                sum1 += A[a+j];
                sum2 += B[a+j];
            }
        }
        if(sum1 <= L) {
            if(sum2 >= P) sum2 = P;
            tmp2[__builtin_popcount(i)].push_back({sum1,sum2});
        }
    }
    long long ans = 0;
    for(int i = min(a,K); i >= 0; i--) {
        vector<pair<int,int>>c,d;
        for(int j = 0; j < tmp1[i].size(); j++) {
            c.push_back(tmp1[i][j]);
        }
        sort(c.rbegin(),c.rend());
        vector<int>cmp;
        for(int j = min(b,K-i); j >= 0; j--) {
            for(int k = 0; k < tmp2[j].size(); k++) {
                d.push_back(tmp2[j][k]);
                cmp.push_back(tmp2[j][k].second);
            }
        }
        sort(d.begin(),d.end());
        sort(cmp.begin(),cmp.end());
        cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end());
        BinaryIndexedTree<int>bit(cmp.size());
        int now = 0;
        for(int j = 0; j < c.size(); j++) {
            while(now < d.size() && c[j].first+d[now].first <= L) {
                int it = lower_bound(cmp.begin(),cmp.end(),d[now].second)-cmp.begin();
                bit.add(it+1,1);
                now++;
            }
            int it = lower_bound(cmp.begin(),cmp.end(),P-c[j].second)-cmp.begin();
            if(it < cmp.size()) {
                ans += bit.sum(it+1,cmp.size());
            }
        }
    }
    cout << ans << endl;
}
0