結果

問題 No.626 Randomized 01 Knapsack
ユーザー ミドリムシミドリムシ
提出日時 2017-12-15 21:45:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,336 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 76,520 KB
実行使用メモリ 199,460 KB
最終ジャッジ日時 2024-05-09 07:54:38
合計ジャッジ時間 7,624 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
198,828 KB
testcase_01 AC 49 ms
198,884 KB
testcase_02 AC 47 ms
198,916 KB
testcase_03 AC 48 ms
198,788 KB
testcase_04 AC 52 ms
198,784 KB
testcase_05 AC 48 ms
198,860 KB
testcase_06 AC 65 ms
198,840 KB
testcase_07 AC 73 ms
198,920 KB
testcase_08 AC 97 ms
198,876 KB
testcase_09 AC 111 ms
198,992 KB
testcase_10 AC 263 ms
198,948 KB
testcase_11 AC 451 ms
199,248 KB
testcase_12 WA -
testcase_13 AC 55 ms
199,236 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 55 ms
199,240 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 53 ms
199,252 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][5000];

long mod_number[5001];

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 / (W / mod_number[n] + 1)] != -1){
        return dp[now][weight_now / (W / mod_number[n] + 1)];
    }else{
        if(weight_now + items[now].weight > W){
            return dp[now][weight_now / (W / mod_number[n] + 1)] = pack(now + 1, weight_now);
        }else{
            return dp[now][weight_now / (W / mod_number[n] + 1)] = 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 < 5000; j++){
            dp[i][j] = -1;
        }
    }
    for(int i = 0; i <= 5000; i++){
        mod_number[i] = 3000;
    }
    mod_number[500] = 2000;
    mod_number[2000] = 4500;
    mod_number[5000] = 4000;
    cout << pack(0, 0) << endl;
}
0