結果

問題 No.626 Randomized 01 Knapsack
ユーザー PachicobuePachicobue
提出日時 2017-12-16 02:44:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,855 bytes
コンパイル時間 2,781 ms
コンパイル使用メモリ 227,376 KB
実行使用メモリ 461,184 KB
最終ジャッジ日時 2024-05-09 09:37:51
合計ジャッジ時間 5,129 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 7 ms
8,832 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 23 ms
24,064 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 211 ms
222,976 KB
testcase_07 AC 216 ms
230,784 KB
testcase_08 AC 429 ms
461,184 KB
testcase_09 AC 411 ms
431,744 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 32 ms
32,896 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 28 ms
27,776 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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;

constexpr ll W_MAX = 30000000;
constexpr ll NW_MAX = 80000000;
constexpr ll N_MAX = 1000;
constexpr ll NVV_MAX = 30000000;
constexpr ll T = 100000000;

inline uint32_t rnd(void)
{
    static uint32_t y = 2463534242;
    y = y ^ (y << 13);
    y = y ^ (y >> 17);
    return y = y ^ (y << 5);
}

bool next(long long b, int t)
{
    if (b > 0) {
        return true;
    }
    return exp(b / 10000.0 / pow(0.99999, t)) > double(rnd()) / numeric_limits<uint32_t>::max();
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    ll W;
    cin >> N >> W;
    using P = pair<ll, ll>;
    vector<P> p;
    ll wsum = 0;
    ll vsum = 0;
    for (int i = 0; i < N; i++) {
        ll w, v;
        cin >> v >> w;
        if (w <= W) {
            wsum += w;
            vsum += v;
            p.push_back(make_pair(v, w));
        }
    }
    if (wsum <= W) {
        cout << vsum << endl;
        return 0;
    }
    if (N == 0) {
        cout << 0 << endl;
        return 0;
    }
    auto comp = [](const P& p1, const P& p2) {
        return __int128_t(p1.first) * __int128_t(p2.second) > __int128_t(p1.second) * __int128_t(p2.first);
    };
    sort(p.begin(), p.end(), comp);
    N = p.size();
    if (W * N < NW_MAX and W < W_MAX) {
        vector<ll> dp(W + 1, 0);
        dp[0] = 0;
        for (int i = 0; i < N; i++) {
            vector<ll> tmp(W + 1, 0);
            for (int j = 0; j <= W; j++) {
                tmp[j] = max(tmp[j], dp[j]);
                if (j + p[i].second <= W) {
                    tmp[j + p[i].second] = max(tmp[j + p[i].second], dp[j] + p[i].first);
                }
            }
            dp = tmp;
        }
        cout << *max_element(dp.begin(), dp.end()) << endl;
    } else {
        ll cap = W;
        ll ans = 0;
        ll rest = N;
        ll used = 0;
        vector<P> res;
        for (used = 0; used < N and rest > N_MAX and rest * cap > NW_MAX; used++) {
            if (cap >= p[used].second) {
                rest--;
                cap -= p[used].second;
                ans += p[used].first;
            } else {
                res.push_back(p[used]);
            }
        }
        for (int i = used; i < N; i++) {
            res.push_back(p[i]);
        }

        if (rest * cap <= NW_MAX) {
            p.clear();
            p.shrink_to_fit();
            vector<ll> dp(cap + 1, 0);
            dp[0] = 0;
            for (int i = 0; i < rest; i++) {
                vector<ll> tmp(cap + 1, 0);
                for (int j = 0; j <= cap; j++) {
                    tmp[j] = max(tmp[j], dp[j]);
                    if (j + res[i].second <= W) {
                        tmp[j + res[i].second] = max(tmp[j + res[i].second], dp[j] + res[i].first);
                    }
                }
                dp = tmp;
            }
            cout << ans + *max_element(dp.begin(), dp.end()) << endl;
            return 0;
        }

        if (rest <= N_MAX) {
            p.clear();
            p.shrink_to_fit();
            ll VMAX = 0;
            for (const auto& e : res) {
                VMAX = max(VMAX, e.first);
            }
            const ll V_SMALL = (ll)sqrt(NVV_MAX / rest);
            const ll K = VMAX / V_SMALL;
            auto rescop = res;
            for (auto& e : rescop) {
                e.first = e.first / K;
            }
            ll vsum = 0;
            for (const auto& e : rescop) {
                vsum += e.first;
            }
            vector<vector<ll>> dp(rest + 1, vector<ll>(vsum + 1, INF<ll>));
            dp[0][0] = 0;
            for (int i = 0; i < rest; i++) {
                for (ll j = 0; j <= vsum; j++) {
                    if (dp[i][j] != INF<ll>) {
                        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
                        dp[i + 1][j + rescop[i].first] = min(dp[i + 1][j + rescop[i].first], dp[i][j] + rescop[i].second);
                    }
                }
            }
            ll maxind = vsum;
            for (ll i = vsum; i >= 0; i--) {
                if (dp[rest][i] <= cap) {
                    maxind = i;
                    break;
                }
            }

            ll pos = maxind;
            for (int i = rest; i >= 1; i--) {
                if (dp[i][pos] != dp[i - 1][pos]) {
                    ans += res[i - 1].first;
                    pos -= rescop[i - 1].first;
                }
            }
            cout << ans << endl;
            return 0;
        }

        ans = 0;
        res.clear();
        res.shrink_to_fit();
        // cap = W;
        // used = 0;
        // vector<int> use;
        // vector<int> unuse;
        // for (used = 0; used < N; used++) {
        //     if (cap >= p[used].second) {
        //         cap -= p[used].second;
        //         ans += p[used].first;
        //         use.push_back(used);
        //     } else {
        //         unuse.push_back(used);
        //     }
        // }

        vector<int> use;
        vector<int> unuse(N);
        iota(unuse.begin(), unuse.end(), 0);

        for (int t = 0; t < T; t++) {
            int k = rnd() % unuse.size();
            int ss = 0;
            long long tt = 0;
            vector<int> del;
            while (tt < p[k].first and ans + p[unuse[k]].second - ss > W) {
                int kk = rnd() % use.size();
                if (find(del.begin(), del.end(), kk) != del.end()) {
                    continue;
                }
                ss += p[use[kk]].second;
                tt += p[use[kk]].first;
                del.emplace_back(kk);
            }
            if (next(p[k].first - tt, t) and ans + p[unuse[k]].second - ss <= W) {
                ans += p[k].second - ss;
                t += p[k].first - tt;
                unuse[k] = unuse.back();
                unuse.pop_back();
                use.emplace_back(k);
                sort(del.rbegin(), del.rend());
                for (int kk : del) {
                    use[kk] = use.back();
                    use.pop_back();
                    unuse.emplace_back(kk);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

0