結果

問題 No.50 おもちゃ箱
ユーザー bal4ubal4u
提出日時 2019-06-02 14:28:48
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 115 ms / 5,000 ms
コード長 1,387 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 30,760 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 23:02:28
合計ジャッジ時間 3,230 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 92 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 57 ms
4,348 KB
testcase_06 AC 10 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 110 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 8 ms
4,348 KB
testcase_22 AC 115 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 8 ms
4,348 KB
testcase_28 AC 70 ms
4,348 KB
testcase_29 AC 70 ms
4,348 KB
testcase_30 AC 68 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 103 ms
4,348 KB
testcase_33 AC 102 ms
4,348 KB
testcase_34 AC 53 ms
4,348 KB
testcase_35 AC 92 ms
4,348 KB
testcase_36 AC 105 ms
4,348 KB
testcase_37 AC 97 ms
4,348 KB
testcase_38 AC 77 ms
4,348 KB
testcase_39 AC 59 ms
4,348 KB
testcase_40 AC 90 ms
4,348 KB
testcase_41 AC 60 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:14:24: note: in expansion of macro 'gc'
   14 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: 50 おもちゃ箱
// 2019.6.2 bal4u

#include <stdio.h>
#include <stdlib.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
int in()    // 非負整数の入力
{
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

int a[15], N;
int b[15], M;
int ans;

void showperm(char *p)
{
	int i, j, s;

	j = 0; for (i = 1; i < ans && i <= M; i++) {
		if (a[p[j]] > b[i]) return;
		s = 0;
		while (j < N && a[p[j]] + s <= b[i]) s += a[p[j++]];
		if (j >= N) {
			if (i < ans) ans = i;
			return;
		}
	}
}

// Kの数{ 1,2,3,4,5,6,7,8,9 } の順列を生成する K!個
void genperm(int K)
{
	int k, t;
	char c[12], *pc, *q;
	static char p[12] = { 1,1,1,1 };

	q = p, pc = c;
	for (k = 1; k <= K; ) *q++ = *pc++ = k++;  // 1 2 3 のように1から
	k = 1, pc = c;
	do {
		t = *(p + k);
		*(p + k) = *(q = p + ((k & 1) ? *pc : 0));
		*q = t;
		showperm(p);
		k = 1, pc = c;
		while (*pc == 0) *pc++ = k++;
		(*pc)--;
	} while (k < K);
}

int cmp(const void *a, const void *b) { return *(int *)b - *(int *)a; }

int main()
{
	int i;
	
	N = in(); for (i = 1; i <= N; i++) a[i] = in();
	qsort(a+1, N, sizeof(int), cmp); a[N+1] = 100;
	M = in(); for (i = 1; i <= M; i++) b[i] = in();
	qsort(b+1, M, sizeof(int), cmp);

	ans = 100;
	if (a[1] <= b[1]) genperm(N);
	if (ans > M) ans = -1;
	printf("%d\n", ans);
	return 0;
}
0