結果
| 問題 |
No.626 Randomized 01 Knapsack
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-15 23:18:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,797 bytes |
| コンパイル時間 | 2,605 ms |
| コンパイル使用メモリ | 201,420 KB |
| 最終ジャッジ日時 | 2025-01-05 05:40:15 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 3 MLE * 14 |
ソースコード
#include <bits/stdc++.h>
#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "sz:" << v.size() << "\n[";
for (const auto& p : v) {
os << p << ",";
}
os << "]\n";
return os;
}
template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
os << "(" << p.first << "," << p.second
<< ")";
return os;
}
constexpr ll MOD = (ll)1e9 + 7LL;
constexpr ld PI = static_cast<ld>(3.1415926535898);
template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int N;
ll W;
cin >> N >> W;
vector<ll> v_(N);
vector<ll> w_(N);
ll wsum = 0;
ll psum = 0;
ll n = 0;
vector<ll> v;
vector<ll> w;
for (int i = 0; i < N; i++) {
cin >> v_[i] >> w_[i];
if (w_[i] <= W) {
wsum += w_[i];
psum += v_[i];
n++;
v.push_back(v_[i]);
w.push_back(w_[i]);
}
}
N = n;
if (wsum <= W) {
cout << psum << endl;
} else {
const ll P = *max_element(v.begin(), v.end());
const ll K = (P * N * N < 100000000 ? 1 : max(1LL, P*9/8 / N));
auto V = v;
for (int i = 0; i < N; i++) {
V[i] /= K;
}
const ll PMAX = accumulate(V.begin(), V.end(), 0LL);
vector<vector<ll>> dp(N + 1, vector<ll>(PMAX + 1, INF<ll>)); //iコ見て価値pで最小重さ
dp[0][0] = 0;
for (ll i = 0; i < N; i++) {
for (ll j = 0; j <= PMAX; j++) {
if (dp[i][j] == INF<ll>) {
continue;
}
if (dp[i + 1][j] > dp[i][j]) {
dp[i + 1][j] = dp[i][j];
}
if (dp[i + 1][j + V[i]] > dp[i][j] + w[i]) {
dp[i + 1][j + V[i]] = dp[i][j] + w[i];
}
}
}
for (ll j = PMAX - 1; j >= 0; j--) {
if (dp[N][j] == INF<ll>) {
dp[N][j] = dp[N][j + 1];
}
}
ll maxind = PMAX;
for (ll i = PMAX; i >= 0; i--) {
if (dp[N][i] <= W) {
maxind = i;
break;
}
}
ll pos = maxind;
ll ans = 0;
for (int i = N; i >= 1; i--) {
const ll next = pos >= V[i - 1] and dp[i][pos] == dp[i - 1][pos - V[i - 1]] + w[i - 1] ? pos - V[i - 1] : pos;
if (next < pos) {
ans += v[i - 1];
}
pos = next;
}
cout << ans << endl;
}
return 0;
}