結果

問題 No.527 ナップサック容量問題
ユーザー Jumbo_kprJumbo_kpr
提出日時 2019-08-01 18:33:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,034 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 96,548 KB
実行使用メモリ 82,600 KB
最終ジャッジ日時 2023-09-18 17:43:39
合計ジャッジ時間 3,311 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,668 KB
testcase_01 AC 4 ms
6,664 KB
testcase_02 AC 4 ms
6,632 KB
testcase_03 AC 3 ms
6,692 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 14 ms
35,424 KB
testcase_06 AC 26 ms
70,260 KB
testcase_07 AC 22 ms
57,844 KB
testcase_08 AC 12 ms
31,284 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 26 ms
72,224 KB
testcase_11 AC 19 ms
51,692 KB
testcase_12 AC 14 ms
37,356 KB
testcase_13 AC 26 ms
72,176 KB
testcase_14 AC 12 ms
29,220 KB
testcase_15 AC 16 ms
43,532 KB
testcase_16 AC 4 ms
6,768 KB
testcase_17 AC 14 ms
37,408 KB
testcase_18 AC 15 ms
41,604 KB
testcase_19 AC 4 ms
8,740 KB
testcase_20 AC 12 ms
29,296 KB
testcase_21 AC 30 ms
82,496 KB
testcase_22 AC 20 ms
55,832 KB
testcase_23 AC 29 ms
80,428 KB
testcase_24 AC 8 ms
18,988 KB
testcase_25 AC 19 ms
53,876 KB
testcase_26 AC 19 ms
49,724 KB
testcase_27 AC 18 ms
49,784 KB
testcase_28 AC 16 ms
43,652 KB
testcase_29 AC 30 ms
82,600 KB
testcase_30 AC 5 ms
10,796 KB
testcase_31 AC 14 ms
37,400 KB
testcase_32 AC 17 ms
45,624 KB
testcase_33 AC 15 ms
39,444 KB
testcase_34 AC 17 ms
45,684 KB
testcase_35 AC 17 ms
45,564 KB
testcase_36 AC 4 ms
6,712 KB
testcase_37 AC 14 ms
35,316 KB
testcase_38 AC 16 ms
43,504 KB
testcase_39 AC 15 ms
41,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC optimize ("-O3","unroll-loops")
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include <stdio.h>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<'\n';
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()
//#define double long double

using namespace std;

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

const int MOD = 1e9 + 7;
const ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int>P;
const int MAX = 200020;


int dp[110][100010];
int v[110], w[110], N, V;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	int sum = 0;
	REP(i, N) {
		cin >> v[i] >> w[i];
		sum += v[i];
	}
	cin >> V;
	REP(i, N) {
		REP(j, 100001) {
			if (j >= w[i]) {
				dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
			}
			else {
				dp[i + 1][j] = dp[i][j];
			}
		}
	}
	int mn = INF, mx = -INF;
	FOR(j,1, 100001) {
		if (dp[N][j] == V) {
			chmin(mn, j);
			chmax(mx, j);
		}
	}
	if (sum == V)mx = -1;
	print(mn);
	if (mx == -1) {
		print("inf");
	}
	else print(mx);
}

0