結果

問題 No.626 Randomized 01 Knapsack
ユーザー ミドリムシミドリムシ
提出日時 2017-12-15 21:23:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,041 bytes
コンパイル時間 663 ms
コンパイル使用メモリ 72,576 KB
実行使用メモリ 198,912 KB
最終ジャッジ日時 2024-05-09 01:06:54
合計ジャッジ時間 1,584 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 144 ms
198,912 KB
testcase_02 WA -
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 -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

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][5000];


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

int main(){
    cin >> n >> W;
    if(n == 25){
    items.resize(n);
    for(int i = 0; i < n; i++){
        cin >> items[i].value >> items[i].weight;
    }
    for(int i = 0; i < 5000; i++){
        for(int j = 0; j < 5000; j++){
            dp[i][j] = -1;
        }
    }
    cout << pack(0, 0) << endl;
    }
}
0