結果

問題 No.527 ナップサック容量問題
ユーザー vvataarnevvataarne
提出日時 2017-06-09 22:36:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,929 bytes
コンパイル時間 1,733 ms
コンパイル使用メモリ 174,840 KB
実行使用メモリ 83,380 KB
最終ジャッジ日時 2023-10-23 22:06:24
合計ジャッジ時間 4,229 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,348 KB
testcase_01 AC 5 ms
8,140 KB
testcase_02 AC 3 ms
6,556 KB
testcase_03 AC 5 ms
8,140 KB
testcase_04 AC 3 ms
5,764 KB
testcase_05 AC 32 ms
36,388 KB
testcase_06 AC 62 ms
71,500 KB
testcase_07 AC 52 ms
58,300 KB
testcase_08 AC 26 ms
31,636 KB
testcase_09 AC 4 ms
5,764 KB
testcase_10 AC 62 ms
72,292 KB
testcase_11 AC 45 ms
51,964 KB
testcase_12 AC 32 ms
38,764 KB
testcase_13 AC 65 ms
73,876 KB
testcase_14 AC 25 ms
29,260 KB
testcase_15 AC 37 ms
44,044 KB
testcase_16 AC 5 ms
8,140 KB
testcase_17 AC 32 ms
37,180 KB
testcase_18 AC 37 ms
41,932 KB
testcase_19 AC 7 ms
8,932 KB
testcase_20 AC 27 ms
30,844 KB
testcase_21 AC 75 ms
83,380 KB
testcase_22 AC 50 ms
56,716 KB
testcase_23 AC 72 ms
81,004 KB
testcase_24 AC 17 ms
19,756 KB
testcase_25 AC 47 ms
55,132 KB
testcase_26 AC 43 ms
49,588 KB
testcase_27 AC 43 ms
50,380 KB
testcase_28 AC 38 ms
44,044 KB
testcase_29 AC 72 ms
82,588 KB
testcase_30 AC 10 ms
12,100 KB
testcase_31 AC 31 ms
37,972 KB
testcase_32 AC 39 ms
45,628 KB
testcase_33 AC 33 ms
39,556 KB
testcase_34 AC 42 ms
47,212 KB
testcase_35 AC 41 ms
47,212 KB
testcase_36 AC 5 ms
7,348 KB
testcase_37 AC 31 ms
36,388 KB
testcase_38 AC 39 ms
44,836 KB
testcase_39 AC 37 ms
41,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  cin >> n;
  vi v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  int V;
  cin >> V;
  int W = 100000;
  vvi dp(n + 1, vi(W + 1));
  for (int i = n - 1; i >= 0; i--) {
    rep(j, W + 1) {
      if (j < w[i]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  int a  = distance(dp[0].begin(), lower_bound(all(dp[0]), V));
  auto b = upper_bound(all(dp[0]), V);
  a += a == 0;
  cout << a << '\n'
       << (b == dp[0].end() ? "inf" : to_string(distance(dp[0].begin(), b) - 1)) << endl;
}
0