結果

問題 No.50 おもちゃ箱
ユーザー fumiphysfumiphys
提出日時 2019-04-10 20:55:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,864 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 169,732 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-22 08:51:55
合計ジャッジ時間 4,133 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 26 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 26 ms
4,380 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 16 ms
4,504 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 22 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 6 ms
4,376 KB
testcase_22 AC 16 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 7 ms
4,380 KB
testcase_28 AC 26 ms
4,380 KB
testcase_29 AC 26 ms
4,376 KB
testcase_30 AC 25 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 26 ms
4,380 KB
testcase_33 AC 26 ms
4,376 KB
testcase_34 AC 27 ms
4,376 KB
testcase_35 AC 26 ms
4,376 KB
testcase_36 AC 26 ms
4,376 KB
testcase_37 AC 26 ms
4,380 KB
testcase_38 AC 26 ms
4,376 KB
testcase_39 AC 26 ms
4,380 KB
testcase_40 AC 26 ms
4,380 KB
testcase_41 AC 26 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes
#include <bits/stdc++.h>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define FI first
#define SE second
using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}

int dp[12][1025];

int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  int m;
  cin >> m;
  vector<int> b(m);
  cin >> b;
  sort(all(b), greater<int>());
  for(int i = 0; i <= m+1; i++){
    for(int j = 0; j <= (1<<n); j++){
      dp[i][j] = -1;
    }
  }
  dp[m][0] = 0;
  for(int i = m - 1; i >= 0; i--){
    for(int j = 0; j < (1 << n); j++){
      dp[i][j] = inf;
      for(int t = j; ; t=(t-1)&j){
        int sum = 0;
        for(int k = 0; k < n; k++){
          if((t >> k) % 2 == 1)sum += a[k];
        }
        if(sum <= b[i] && dp[i+1][j & (~t)] >= 0){
          dp[i][j] = min(dp[i][j], (sum == 0 ? 0: 1) + dp[i+1][j & (~t)]);
        }
        if(t == 0)break;
      }
      if(dp[i][j] == inf)dp[i][j] = -1;
    }
  }
  cout << dp[0][(1<<n)-1] << endl;
	return 0;
}
0