結果

問題 No.2167 Fibonacci Knapsack
ユーザー hotman78hotman78
提出日時 2022-12-19 08:36:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,565 ms / 2,000 ms
コード長 1,198 bytes
コンパイル時間 2,624 ms
コンパイル使用メモリ 212,360 KB
実行使用メモリ 7,184 KB
最終ジャッジ日時 2023-08-11 09:32:07
合計ジャッジ時間 30,159 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1,076 ms
5,832 KB
testcase_04 AC 1,077 ms
5,116 KB
testcase_05 AC 1,308 ms
7,184 KB
testcase_06 AC 1,220 ms
6,236 KB
testcase_07 AC 1,436 ms
6,096 KB
testcase_08 AC 1,565 ms
6,200 KB
testcase_09 AC 1,324 ms
6,808 KB
testcase_10 AC 1,333 ms
6,744 KB
testcase_11 AC 1,372 ms
6,288 KB
testcase_12 AC 1,504 ms
6,128 KB
testcase_13 AC 1,423 ms
6,836 KB
testcase_14 AC 1,290 ms
6,464 KB
testcase_15 AC 1,174 ms
6,508 KB
testcase_16 AC 1,403 ms
6,520 KB
testcase_17 AC 1,297 ms
6,016 KB
testcase_18 AC 1,188 ms
5,452 KB
testcase_19 AC 1,139 ms
5,696 KB
testcase_20 AC 1,370 ms
6,708 KB
testcase_21 AC 1,419 ms
5,968 KB
testcase_22 AC 1,252 ms
6,032 KB
testcase_23 AC 695 ms
5,772 KB
権限があれば一括ダウンロードができます

ソースコード

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;
        lint ans=0;
        dp[w]=0;
        for(lint i=n-1;i>=0;i--){
            map<lint,lint>dp1;
            for(auto [e,c]:dp){
                if(i!=0&&e-a[i]-a[i-1]>=0){
                    dp1[e-a[i]]=max(dp1[e-a[i]],c+f[i]);
                    ans=max(ans,c+f[i]);
                }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);
        }
        cout<<ans<<endl;
    }
}
0