結果

問題 No.527 ナップサック容量問題
ユーザー 👑 jupirojupiro
提出日時 2020-01-16 03:39:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,661 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 116,896 KB
実行使用メモリ 83,032 KB
最終ジャッジ日時 2023-09-05 04:02:58
合計ジャッジ時間 4,340 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,032 KB
testcase_01 AC 6 ms
7,816 KB
testcase_02 AC 4 ms
6,268 KB
testcase_03 AC 6 ms
7,808 KB
testcase_04 AC 4 ms
5,508 KB
testcase_05 AC 34 ms
36,044 KB
testcase_06 AC 71 ms
71,084 KB
testcase_07 AC 58 ms
58,032 KB
testcase_08 AC 29 ms
31,320 KB
testcase_09 AC 4 ms
5,548 KB
testcase_10 AC 73 ms
71,924 KB
testcase_11 AC 52 ms
51,880 KB
testcase_12 AC 37 ms
38,440 KB
testcase_13 AC 74 ms
73,552 KB
testcase_14 AC 27 ms
29,056 KB
testcase_15 AC 43 ms
43,932 KB
testcase_16 AC 6 ms
7,772 KB
testcase_17 AC 35 ms
36,904 KB
testcase_18 AC 41 ms
41,324 KB
testcase_19 AC 7 ms
8,664 KB
testcase_20 AC 28 ms
30,528 KB
testcase_21 AC 84 ms
83,032 KB
testcase_22 AC 56 ms
56,372 KB
testcase_23 AC 80 ms
80,676 KB
testcase_24 AC 17 ms
19,408 KB
testcase_25 AC 54 ms
54,856 KB
testcase_26 AC 49 ms
49,260 KB
testcase_27 AC 50 ms
50,024 KB
testcase_28 AC 43 ms
43,660 KB
testcase_29 AC 83 ms
82,260 KB
testcase_30 AC 10 ms
11,768 KB
testcase_31 AC 35 ms
37,696 KB
testcase_32 AC 43 ms
45,360 KB
testcase_33 AC 38 ms
39,268 KB
testcase_34 AC 45 ms
47,008 KB
testcase_35 AC 47 ms
46,888 KB
testcase_36 AC 6 ms
7,076 KB
testcase_37 AC 35 ms
36,020 KB
testcase_38 AC 43 ms
44,484 KB
testcase_39 AC 40 ms
41,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;


int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	const ll MAX = 100050;
	ll N; scanf("%lld", &N);
	vector<ll> v(N), w(N);
	for (ll i = 0; i < N; i++) scanf("%lld %lld", &v[i], &w[i]);
	ll V; scanf("%lld", &V);
	vector<vector<ll>> dp(N + 1, vector<ll>(MAX, -1));
	dp[0][0] = 0;
	ll sum = 0;
	for (ll i = 0; i < N; i++) {
		sum += w[i];
		for (ll j = 0; j < MAX; j++) {
			chmax(dp[i + 1][j], dp[i][j]);
			if (dp[i][j] == -1) continue;
			chmax(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
		}
	}

	ll mn = INF;
	ll mx = 1;
	for (ll i = 0; i < MAX - 1; i++) {
		chmax(dp[N][i + 1], dp[N][i]);
		if (i == 0) continue;
		if (dp[N][i] == V) {
			chmin(mn, i);
			chmax(mx, i);
		}
	}
	if (mx >= sum) {
		cout << mn << "\n" << "inf" << endl;
	}
	else {
		cout << mn << "\n" << mx << endl;

	}
	return 0;
}
0