結果

問題 No.2167 Fibonacci Knapsack
ユーザー 👑 Nachia
提出日時 2022-12-19 00:22:45
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,517 bytes
コンパイル時間 2,739 ms
コンパイル使用メモリ 127,708 KB
最終ジャッジ日時 2025-02-09 16:31:12
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <atcoder/modint>

using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

using Modint = atcoder::static_modint<998244353>;

u64 testcase(){
    int N; cin >> N;
    u64 W; cin >> W;
    vector<u64> w(N); rep(i,N) cin >> w[i];
    vector<u64> v(N); v[0] = 1; v[1] = 2; for(int i=2; i<N; i++) v[i] = v[i-1] + v[i-2];
    map<u64, u64> dptab = { { 0, W } };
    auto dpback = [&]() -> pair<u64, u64> {
        auto i = dptab.end();
        i--;
        return *i;
    };
    auto dpins = [&](map<u64, u64>& buf, u64 v, u64 w) -> void {
        buf[v] = max(buf[v], w);
    };
    u64 ans = 0;
    for(int i=N-1; i>=0; i--){
        u64 maxVsum = 0; for(int j=0; j<=i; j++) maxVsum += v[j];
        u64 dpmaxVsum = dpback().first;
        map<u64, u64> buf;
        for(auto [vv, ww] : dptab){
            if(vv + maxVsum <= dpmaxVsum) continue;
            dpins(buf, vv, ww);
            if(ww >= w[i]) dpins(buf, vv + v[i], ww - w[i]);
        }
        swap(dptab, buf);
    }
    ans = dpback().first;
    return ans;
}

int main(){
    int T; cin >> T;
    rep(t,T) cout << testcase() << '\n';
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0