結果

問題 No.27 板の準備
ユーザー not_522not_522
提出日時 2015-08-19 15:42:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,255 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 150,408 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-27 04:07:02
合計ジャッジ時間 3,104 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<typename Weight, typename Value, bool strict = false> Value unboundedKnapsack(const vector<Weight>& maxWeight, const vector<Weight>& weight, const vector<Value>& value) {
  constexpr Value IMP = numeric_limits<Value>::min() + 1;
  const Weight mx = *max_element(maxWeight.begin(), maxWeight.end());
  vector<Value> dp(mx + Weight(1));
  if (strict) fill(dp.begin() + 1, dp.end(), IMP);
  for (size_t i = 0; i < weight.size(); ++i) {
    for (int w = 0; w <= mx; ++w) {
      if (strict && dp[w] == IMP) continue;
      Weight ww = Weight(w) + weight[i];
      Value vv = dp[w] + value[i];
      if (ww <= mx && dp[ww] < vv) dp[ww] = vv;
    }
  }
  Value res = 0;
  for (const auto& w : maxWeight) {
    if (dp[w] == IMP) return IMP;
    res += dp[w];
  }
  return res;
}

template<typename Weight, typename Value, bool strict = false> Value unboundedKnapsack(Weight maxWeight, const vector<Weight>& weight, const vector<Value>& value) {
  return unboundedKnapsack({maxWeight}, weight, value);
}

template<typename Weight, typename Value = long long> vector<Value> knapsackCount(Weight maxWeight, const vector<Weight>& weight) {
  vector<Value> dp(maxWeight + Weight(1));
  dp[0] = 1;
  for (auto& w : weight) {
    for (int i = 0; i <= maxWeight; ++i) {
      Weight ww = Weight(i) + w;
      if (ww <= maxWeight) dp[ww] += dp[i];
    }
  }
  return dp;
}

template<typename Weight> vector<bool> unboundedKnapsackFill(Weight maxWeight, const vector<Weight>& weight) {
  vector<bool> dp(maxWeight + Weight(1));
  dp[0] = true;
  for (auto& w : weight) {
    for (int i = 0; i <= maxWeight; ++i) {
      Weight ww = Weight(i) + w;
      if (ww <= maxWeight && dp[i]) dp[ww] = true;
    }
  }
  return dp;
}

int main() {
  vector<int> v(4);
  for (int& i : v) cin >> i;
  sort(v.begin(), v.end());
  if (v.back() <= 3) {
    cout << 4 << endl;
    return 0;
  }
  vector<int> a(3), b(3, -1);
  int res = numeric_limits<int>::max();
  for (a[0] = 1; a[0] <= v.back(); ++a[0]) {
    for (a[1] = a[0] + 1; a[1] <= v.back(); ++a[1]) {
      for (a[2] = a[1] + 1; a[2] <= v.back(); ++a[2]) {
        res = min(res, -unboundedKnapsack<int, int, true>(v, a, b));
      }
    }
  }
  cout << res << endl;
}
0