結果

問題 No.50 おもちゃ箱
ユーザー satonakatakumisatonakatakumi
提出日時 2021-08-22 15:17:02
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,342 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 34,176 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 04:32:14
合計ジャッジ時間 1,934 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
5,376 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 WA -
testcase_22 WA -
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#include <assert.h>

typedef int64_t i64;
typedef uint64_t u64;
typedef __int128_t i128;
typedef __uint128_t u128;

void bucket_sort(i64 arr[], i64 sz)
{
  i64 buckets[21];
  i64 i;
  for (i = 0; i < 21; i++)
    buckets[i] = 0;
  for (i = 0; i < sz; i++)
    buckets[arr[i]] = arr[i];
  i64 j = 0;
  for (i = 0; i < 21; i++)
    if (0 < buckets[i])
      arr[j++] = buckets[i];
}

int main()
{
  int i, j, k;
  int N;
  scanf("%d", &N);
  i64 a[N];
  for (i = 0; i < N; i++)
  {
    scanf("%ld", &a[i]);
  }
  int M;
  scanf("%d", &M);
  i64 b[M];
  for (i = 0; i < M; i++)
  {
    scanf("%ld", &b[i]);
  }
  bucket_sort(b, M);
  bool dp1[1 << 10];
  bool dp2[1 << 10];
  dp1[0] = true;
  for (i = 0; i < M; i++)
  {
    for (j = 0; j < (1 << N); j++)
    {
      i64 c = 0;
      for (k = 0; k < N; k++)
      {
        if ((j >> k) % 2 == 0)
          continue;
        c += a[k];
      }
      if (c > b[M - 1 - i])
        continue;
      for (k = (1 << N) - 1; k >= 0; k--)
      {
        dp2[j | k] |= dp1[k];
      }
    }
    for (k = (1 << N) - 1; k >= 0; k--)
    {
      dp1[k] = dp2[k];
    }
    if (dp1[(1 << N) - 1])
    {
      printf("%d\n", i + 1);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
0