結果

問題 No.393 2本の竹
ユーザー te-shte-sh
提出日時 2017-07-27 11:02:49
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,257 bytes
コンパイル時間 1,110 ms
コンパイル使用メモリ 97,240 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-03 15:32:16
合計ジャッジ時間 3,526 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,388 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags
import std.bitmanip;  // BitArray

void main()
{
  auto d = readln.chomp.to!size_t;
  foreach (_; 0..d) writeln(calc());
}

auto calc()
{
  auto n = readln.split.to!(int[]), maxN = n.maxElement;
  auto m = readln.chomp.to!size_t;
  auto a = readln.split.to!(int[]);

  a.sort();

  auto w = new int[](n.length), k = 0;
  foreach (i, ni; n) {
    auto r = fill(ni, a[k..$]);
    k += r[0];
    w[i] = r[1];
  }
  auto x = w.sum - a[k];

  if (k == m) return m;
  if (x < 0) return k;

  auto dp1 = BitArray(), dp2 = BitArray();
  dp1.length = maxN + 1;
  dp2.length = maxN + 1;
  dp1[0] = true;

  foreach (i; 0..k) {
    (cast(size_t[]) dp2)[] = (cast(size_t[]) dp1)[];
    lshift(dp2, a[i]);
    dp1 |= dp2;
  }

  foreach (ni; n)
    foreach (i; max(ni-x, 0)..ni+1)
      if (dp1[i]) return k+1;

  return k;
}

auto fill(int n, int[] a)
{
  foreach (i, ai; a)
    if (n > ai)
      n -= ai;
    else
      return Tuple!(size_t, int)(i, n);

  return Tuple!(size_t, int)(a.length, n);
}

auto lshift(ref BitArray ba, size_t i)
{
  if (i % 64 == 0) {
    if (i > 0) {
      ba <<= 1;
      ba <<= i-1;
    }
  } else {
    ba <<= i;
  }
}
0