結果

問題 No.626 Randomized 01 Knapsack
ユーザー PachicobuePachicobue
提出日時 2017-12-15 22:52:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,616 bytes
コンパイル時間 3,021 ms
コンパイル使用メモリ 207,000 KB
実行使用メモリ 817,152 KB
最終ジャッジ日時 2024-05-09 08:30:12
合計ジャッジ時間 4,780 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
6,912 KB
testcase_04 AC 5 ms
7,424 KB
testcase_05 AC 28 ms
34,560 KB
testcase_06 AC 202 ms
234,240 KB
testcase_07 MLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 *2/ 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;
}
0