結果

問題 No.2866 yuusaan's Knapsack
ユーザー yuusaanyuusaan
提出日時 2024-07-18 09:12:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,230 bytes
コンパイル時間 2,914 ms
コンパイル使用メモリ 253,560 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-08-18 19:23:41
合計ジャッジ時間 4,243 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 WA -
testcase_11 RE -
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 -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

pair<int,int> pairmax(pair<int,int> a,pair<int,int> b){
    if(b > a)swap(a,b);
    if(a.first > b.first){
        return a;
    }
    else{
        return {a.first,a.second + b.second};
    }
}

int main(){
    int inf = 2e9;
    int N,W;
    cin >> N >> W;
    vector<int> v(N+1),w(N+1);
    for(int i = 1;i <= N;i++){
        cin >> v[i] >> w[i];
    }
    int crit = 1000;
    vector<vector<pair<int,int>>> dp(N+1,vector<pair<int,int>> (2001,{-inf,1}));
    for(int i = 0;i <= N;i++){
    for(int j = -1000;j < 0;j++){
        dp[i][j+crit] = {-inf,0};
    }
    }
    pair<int,int> ans = {0,0};
    dp[0][0+crit] = {0,1};
    for(int i = 1;i <= N;i++){
        for(int j = -1000;j <= 1000;j++)dp[i][j+crit] = dp[i-1][j+crit];
        for(int j = -1000;j <= 1000;j++){
            if(-1000 <= j - w[i] && j - w[i] <= 1000){
                if(dp[i-1][j-w[i]+crit].first != -inf)dp[i][j + crit] = pairmax(dp[i][j + crit],{dp[i-1][j-w[i] + crit].first + v[i],dp[i-1][j-w[i] + crit].second});
            }
        }
    }

    
    for(int j = -1000;j <= W;j++){
        ans = pairmax(ans,dp[N][j+crit]);
    }



    cout << ans.first << " " << ans.second << endl;
    return 0;
}
0