結果
| 問題 |
No.626 Randomized 01 Knapsack
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-15 22:44:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,743 bytes |
| コンパイル時間 | 2,389 ms |
| コンパイル使用メモリ | 205,576 KB |
| 最終ジャッジ日時 | 2025-01-05 05:33:44 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 MLE * 19 |
ソースコード
#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 ld EPS_INV = 1.5;
// constexpr ll K = max(1LL,)
const ll K = max(1LL, (ll)(P / EPS_INV / 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で最小重さ
vector<vector<int>> prev(N + 1, vector<int>(PMAX + 1, -1));
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];
prev[i + 1][j] = j;
}
if (dp[i + 1][j + V[i]] > dp[i][j] + w[i]) {
dp[i + 1][j + V[i]] = dp[i][j] + w[i];
prev[i + 1][j + V[i]] = j;
}
}
}
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 = prev[i][pos];
if (next < pos) {
ans += v[i - 1];
}
pos = next;
}
cout << ans << endl;
}
return 0;
}