結果
問題 |
No.626 Randomized 01 Knapsack
|
ユーザー |
|
提出日時 | 2017-12-15 22:52:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,616 bytes |
コンパイル時間 | 2,111 ms |
コンパイル使用メモリ | 200,884 KB |
最終ジャッジ日時 | 2025-01-05 05:36:18 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 4 WA * 3 MLE * 18 |
ソースコード
#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; for (int i = 0; i < N; i++) { cin >> v[i] >> w[i]; wsum += w[i]; psum += v[i]; } if (wsum <= W) { cout << psum << endl; } else { const ll P = *max_element(v.begin(), v.end()); // constexpr ll EPS_INV = 1; const ll K = max(1LL, (ll)(P *5/ 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; }