結果

問題 No.527 ナップサック容量問題
ユーザー nebukuro09nebukuro09
提出日時 2017-06-09 22:44:27
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,191 bytes
コンパイル時間 1,111 ms
コンパイル使用メモリ 118,568 KB
実行使用メモリ 42,772 KB
最終ジャッジ日時 2023-09-03 14:07:06
合計ジャッジ時間 3,545 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,384 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 3 ms
5,168 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 19 ms
20,100 KB
testcase_06 AC 39 ms
35,992 KB
testcase_07 AC 32 ms
30,804 KB
testcase_08 AC 17 ms
16,008 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 39 ms
36,324 KB
testcase_11 AC 28 ms
26,888 KB
testcase_12 AC 20 ms
20,376 KB
testcase_13 AC 40 ms
37,380 KB
testcase_14 AC 15 ms
16,232 KB
testcase_15 AC 23 ms
22,428 KB
testcase_16 AC 3 ms
4,944 KB
testcase_17 AC 20 ms
19,124 KB
testcase_18 AC 22 ms
21,832 KB
testcase_19 AC 4 ms
5,740 KB
testcase_20 AC 18 ms
17,156 KB
testcase_21 AC 45 ms
42,260 KB
testcase_22 AC 32 ms
29,628 KB
testcase_23 AC 44 ms
41,960 KB
testcase_24 AC 11 ms
10,420 KB
testcase_25 AC 30 ms
28,688 KB
testcase_26 AC 27 ms
26,364 KB
testcase_27 AC 28 ms
26,164 KB
testcase_28 AC 23 ms
22,380 KB
testcase_29 AC 44 ms
42,772 KB
testcase_30 AC 6 ms
6,216 KB
testcase_31 AC 20 ms
20,112 KB
testcase_32 AC 25 ms
22,976 KB
testcase_33 AC 22 ms
20,928 KB
testcase_34 AC 26 ms
23,724 KB
testcase_35 AC 26 ms
23,772 KB
testcase_36 AC 3 ms
4,936 KB
testcase_37 AC 20 ms
18,928 KB
testcase_38 AC 23 ms
22,676 KB
testcase_39 AC 22 ms
21,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

alias Tuple!(int, "v", int, "w") Knap;

void main() {
    auto N = readln.chomp.to!int;
    auto K = new Knap[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!int);
        K[i] = Knap(s[0], s[1]);
    }
    auto V = readln.chomp.to!int;


    K.sort!"a.w < b.w"();

    auto dp = new int[][](N, 10^^5+1);
    dp[0][K[0].w] = K[0].v;

    foreach (i; 1..N) {
        foreach (k; 0..10^^5+1) {
            dp[i][k] = max(dp[i][k], dp[i-1][k]);
            if (k + K[i].w < 10^^5+1)
                dp[i][k+K[i].w] = max(dp[i][k+K[i].w], dp[i-1][k] + K[i].v);
        }
        dp[i][K[i].w] = max(dp[i][K[i].w], K[i].v);
    }

    foreach (j; 0..10^^5) dp[N-1][j+1] = max(dp[N-1][j], dp[N-1][j+1]);

    int minv = 1 << 29;
    int maxv = 0;
    foreach (j; 0..10^^5+1) {
        if (dp[N-1][j] == V) minv = min(minv, j), maxv = max(maxv, j);
    }

    if (K.map!(k => k.v).sum == V) maxv = -1;
    writeln(max(minv, 1));
    writeln(maxv == -1 ? "inf" : maxv.to!string);
}
0