結果

問題 No.527 ナップサック容量問題
ユーザー jupiro
提出日時 2020-01-16 03:39:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,661 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 118,020 KB
実行使用メモリ 83,200 KB
最終ジャッジ日時 2024-06-23 00:43:31
合計ジャッジ時間 3,765 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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