結果

問題 No.393 2本の竹
ユーザー nebukuro09
提出日時 2017-01-03 18:02:45
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 178 ms / 1,000 ms
コード長 1,050 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 126,684 KB
実行使用メモリ 59,240 KB
最終ジャッジ日時 2024-06-12 06:15:05
合計ジャッジ時間 2,691 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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;

void solve() {
    auto n = readln.split.map!(to!int).array.sort();
    auto m = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array.sort();
    auto acm = new int[](m);
    acm[0] = A[0];
    foreach (i; 1..m) acm[i] = acm[i-1] + A[i];

    auto dp = new int[][](m, n[0]+1);
    if (A[0] <= n[0]) dp[0][n[0]-A[0]] = 1;
    if (A[0] <= n[1]) dp[0][n[0]] = 1;

    foreach (i; 1..m) {
        foreach (j; 0..n[0]+1) { // j = 1本目の竹の残り
            int k = n[1] - (acm[i-1] - (n[0] - j)); // k = 2本目の竹の残り
            if (A[i] <= j) dp[i][j-A[i]] = max(dp[i][j-A[i]], dp[i-1][j] + 1);
            if (A[i] <= k) dp[i][j] = max(dp[i][j], dp[i-1][j] + 1);
        }
    }

    dp.map!(x => x.reduce!(max)).reduce!(max).writeln;
}

void main() {
    auto d = readln.chomp.to!int;
    foreach (i; 0..d)
        solve;
}
0