結果

問題 No.626 Randomized 01 Knapsack
ユーザー ミドリムシミドリムシ
提出日時 2017-12-15 21:33:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,535 bytes
コンパイル時間 760 ms
コンパイル使用メモリ 78,164 KB
実行使用メモリ 199,292 KB
最終ジャッジ日時 2024-05-09 06:15:42
合計ジャッジ時間 21,739 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
198,700 KB
testcase_01 AC 48 ms
198,880 KB
testcase_02 AC 47 ms
198,844 KB
testcase_03 AC 48 ms
198,708 KB
testcase_04 AC 57 ms
198,820 KB
testcase_05 AC 47 ms
198,816 KB
testcase_06 AC 107 ms
198,908 KB
testcase_07 AC 199 ms
198,908 KB
testcase_08 AC 228 ms
198,760 KB
testcase_09 AC 493 ms
198,952 KB
testcase_10 AC 625 ms
199,028 KB
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 AC 1,007 ms
199,080 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 51 ms
199,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct item{
    long value, weight;
};

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

long dp[5000][5000];

double mod_number_n[5001];
long mod_number_weight[5001];

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