結果

問題 No.1861 Required Number
ユーザー tnakao0123tnakao0123
提出日時 2022-03-04 23:22:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,500 ms
コード長 1,131 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 42,644 KB
実行使用メモリ 22,592 KB
最終ジャッジ日時 2023-09-26 03:55:38
合計ジャッジ時間 3,988 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,068 KB
testcase_01 AC 2 ms
4,992 KB
testcase_02 AC 2 ms
5,068 KB
testcase_03 AC 37 ms
22,592 KB
testcase_04 AC 36 ms
22,560 KB
testcase_05 AC 2 ms
5,036 KB
testcase_06 AC 2 ms
5,084 KB
testcase_07 AC 2 ms
5,044 KB
testcase_08 AC 2 ms
5,296 KB
testcase_09 AC 2 ms
4,988 KB
testcase_10 AC 2 ms
5,024 KB
testcase_11 AC 2 ms
5,072 KB
testcase_12 AC 2 ms
4,980 KB
testcase_13 AC 2 ms
5,116 KB
testcase_14 AC 2 ms
5,060 KB
testcase_15 AC 2 ms
5,060 KB
testcase_16 AC 2 ms
4,964 KB
testcase_17 AC 2 ms
5,060 KB
testcase_18 AC 2 ms
5,116 KB
testcase_19 AC 3 ms
5,124 KB
testcase_20 AC 2 ms
5,088 KB
testcase_21 AC 2 ms
5,108 KB
testcase_22 AC 2 ms
5,156 KB
testcase_23 AC 2 ms
5,048 KB
testcase_24 AC 8 ms
15,292 KB
testcase_25 AC 16 ms
21,972 KB
testcase_26 AC 8 ms
17,272 KB
testcase_27 AC 10 ms
21,408 KB
testcase_28 AC 10 ms
21,396 KB
testcase_29 AC 10 ms
21,676 KB
testcase_30 AC 9 ms
21,992 KB
testcase_31 AC 16 ms
21,748 KB
testcase_32 AC 9 ms
21,820 KB
testcase_33 AC 13 ms
21,680 KB
testcase_34 AC 15 ms
21,724 KB
testcase_35 AC 15 ms
22,384 KB
testcase_36 AC 9 ms
21,768 KB
testcase_37 AC 12 ms
22,460 KB
testcase_38 AC 14 ms
21,592 KB
testcase_39 AC 12 ms
21,640 KB
testcase_40 AC 16 ms
22,556 KB
testcase_41 AC 16 ms
22,040 KB
testcase_42 AC 14 ms
22,588 KB
testcase_43 AC 12 ms
22,132 KB
testcase_44 AC 2 ms
5,072 KB
04_evil_1.txt RE -
04_evil_2.txt RE -
04_evil_3.txt RE -
04_evil_4.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1861.cc:  No.1861 Required Number - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 10000;
const int MAX_K = 1000;

/* typedef */

/* global variables */

int as[MAX_N];
bool dp0[MAX_N + 1][MAX_K + 1], dp1[MAX_N + 1][MAX_K + 1];

/* subroutines */

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  dp0[0][0] = true;
  for (int i = 0; i < n; i++) {
    copy(dp0[i], dp0[i] + k + 1, dp0[i + 1]);
    for (int j = k - as[i]; j >= 0; j--)
      if (dp0[i][j]) dp0[i + 1][j + as[i]] = true;
  }

  if (! dp0[n][k]) { puts("-1"); return 0; }

  dp1[n][0] = true;
  for (int i = n - 1; i >= 0; i--) {
    copy(dp1[i + 1], dp1[i + 1] + k + 1, dp1[i]);
    for (int j = k - as[i]; j >= 0; j--)
      if (dp1[i + 1][j]) dp1[i][j + as[i]] = true;
  }

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    bool found = false;
    for (int j = 0; ! found && j <= k; j++)
      found = (dp0[i][j] && dp1[i + 1][k - j]);

    if (! found) cnt++;
  }

  printf("%d\n", cnt);
  return 0;
}
0