結果

問題 No.2167 Fibonacci Knapsack
ユーザー 👑 NachiaNachia
提出日時 2022-12-19 00:22:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,517 bytes
コンパイル時間 1,253 ms
コンパイル使用メモリ 95,016 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 09:21:40
合計ジャッジ時間 2,409 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,384 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 4 ms
4,384 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 4 ms
4,384 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 4 ms
4,384 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 4 ms
4,384 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 3 ms
4,384 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 4 ms
4,384 KB
testcase_21 AC 4 ms
4,384 KB
testcase_22 AC 4 ms
4,384 KB
testcase_23 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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