結果

問題 No.2167 Fibonacci Knapsack
ユーザー hotman78hotman78
提出日時 2022-12-19 08:34:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,257 bytes
コンパイル時間 2,401 ms
コンパイル使用メモリ 213,984 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 02:03:49
合計ジャッジ時間 6,032 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma optimize("Ofast")
#pragma target("avx2")
#include<bits/stdc++.h>
using namespace std;
using lint = long long;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define endl '\n'

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--){
        lint n,w;
        cin>>n>>w;
        vector<lint>a(n),b(n+1);
        rep(i,0,n)cin>>a[i];
        rep(i,0,n)b[i+1]=b[i]+a[i];
        vector<lint>f(n);
        f[0]=1;
        if(n>=2)f[1]=2;
        rep(i,2,n)f[i]=f[i-1]+f[i-2];
        map<lint,lint>dp,dp1;
        lint ans=0;
        dp[w]=0;
        for(lint i=n-1;i>=0;i--){
            map<lint,lint>dp2;
            for(auto [e,c]:dp){
                if(i!=0&&e-a[i]-a[i-1]>=0){
                    dp2[e-a[i]-a[i-1]]=max(dp2[e-a[i]-a[i-1]],c+f[i]+f[i-1]);
                    ans=max(ans,c+f[i]+f[i-1]);
                }else{
                    bool ok=0;
                    if(e-a[i]>=0){
                        ok=1;
                        ans=max(ans,c+f[i]);
                        dp1[e-a[i]]=max(dp1[e-a[i]],c+f[i]);
                    }
                    dp1[e]=max(dp1[e],c);
                }
            }
            swap(dp,dp1);
            swap(dp1,dp2);
        }
        cout<<ans<<endl;
    }
}
0