結果

問題 No.626 Randomized 01 Knapsack
ユーザー ミドリムシミドリムシ
提出日時 2017-12-15 21:48:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,151 bytes
コンパイル時間 1,045 ms
コンパイル使用メモリ 75,088 KB
実行使用メモリ 277,396 KB
最終ジャッジ日時 2023-08-22 02:09:49
合計ジャッジ時間 11,639 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
276,772 KB
testcase_01 AC 107 ms
276,828 KB
testcase_02 AC 152 ms
276,768 KB
testcase_03 AC 73 ms
276,776 KB
testcase_04 AC 75 ms
276,792 KB
testcase_05 AC 68 ms
276,816 KB
testcase_06 AC 104 ms
276,836 KB
testcase_07 AC 105 ms
276,868 KB
testcase_08 AC 137 ms
276,936 KB
testcase_09 AC 138 ms
276,892 KB
testcase_10 AC 325 ms
277,172 KB
testcase_11 AC 613 ms
277,132 KB
testcase_12 AC 759 ms
277,084 KB
testcase_13 WA -
testcase_14 AC 229 ms
277,076 KB
testcase_15 WA -
testcase_16 AC 728 ms
277,228 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 77 ms
277,084 KB
testcase_22 AC 419 ms
277,084 KB
testcase_23 AC 624 ms
277,132 KB
testcase_24 AC 74 ms
277,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct item{
    long value, weight;
};

int n;
long W;
vector<item> items;

long dp[5000][7000];

long mod_number;

bool compare(item item1, item item2){
    return item1.weight > item2.weight;
}

long pack(int now, long weight_now){
    if(now == n){
        return 0;
    }else if(dp[now][weight_now / mod_number] != -1){
        return dp[now][weight_now / mod_number];
    }else{
        if(weight_now + items[now].weight > W){
            return dp[now][weight_now / mod_number] = pack(now + 1, weight_now);
        }else{
            return dp[now][weight_now / mod_number] = max(pack(now + 1, weight_now), items[now].value + pack(now + 1, weight_now + items[now].weight));
        }
    }
}

int main(){
    cin >> n >> W;
    items.resize(n);
    for(int i = 0; i < n; i++){
        cin >> items[i].value >> items[i].weight;
    }
    sort(items.begin(), items.end(), &compare);
    for(int i = 0; i < 5000; i++){
        for(int j = 0; j < 7000; j++){
            dp[i][j] = -1;
        }
    }
    mod_number = W / 7000 + 1;
    cout << pack(0, 0) << endl;
}
0