結果

問題 No.2161 Black Market
ユーザー Lanatus
提出日時 2022-12-15 20:26:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,908 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 209,116 KB
最終ジャッジ日時 2025-02-09 13:59:00
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define all(v) begin(v), end(v)

using namespace std;

const int MOD = 1e9 + 7;

using ull = unsigned long long;
using ll  = long long;
using ld  = long double;

template <class S, class T> inline bool chmax(S &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class S, class T> inline bool chmin(S &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <class S> using pq  = priority_queue<S>;
template <class S> using pqg = priority_queue<S, vector<S>, greater<S>>;


int main() {
  int n, k;
  ll l, p;
  cin >> n >> k >> l >> p;

  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];

  vector<map<ll,vector<ll>>> cnt(n);
  for (int bit = 0; bit < (1 << (n / 2)); ++bit) {
    bool ok = true;
    int num = 0;
    ll cost = 0, value = 0;
    
    for (int i = 0; i < n / 2; ++i) {
      if (num > k || cost > l) {
        ok = false;
        break;
      }

      if (bit & (1 << i)) {
        ++num;
        cost += a[i];
        value += b[i];
      }
    }

    if (ok) cnt[num][cost].push_back(value);
  }

  for (int i = 0; i < n; ++i) {
    for (auto &&[a, v] : cnt[i]) {
      sort(all(v));
    }
  }

  ll ans = 0;

  for (int bit = 0; bit < (1 << (n - n / 2)); ++bit) {
    bool ok = true;
    int num = 0;
    ll cost = 0, value = 0;

    for (int i = 0; i < (n - n / 2); ++i) {
      if (num > k || cost > l) {
        ok = false;
        break;
      }

      if (bit & (1 << i)) {
        ++num;
        cost += a[i + n / 2];
        value += b[i + n / 2];
      }
    }

    if (!ok) continue;

    for (int i = 0; i <= k - num; ++i) {
      if (cnt[i].empty()) continue;

      for (auto &&[a, v] : cnt[i]) {
        if (a > (l - cost)) break;
        if (v.empty()) continue;

        ans += v.end() - lower_bound(all(v), p - value);
      }
    }
  }

  cout << ans << endl;
  return 0;
}
0