結果

問題 No.617 Nafmo、買い出しに行く
ユーザー bal4ubal4u
提出日時 2019-05-19 21:10:38
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,450 bytes
コンパイル時間 1,161 ms
コンパイル使用メモリ 31,220 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 07:59:29
合計ジャッジ時間 1,264 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 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 1 ms
4,348 KB
testcase_06 AC 1 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 2 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 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 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: 617 Nafmo、買い出しに行く
// 2019.5.19 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[22], w;
int a[362900], aw;

// 見つからなければ、一つ小さい要素を返す
int bsch(int x)
{
	int m, l = 0, r = aw;

    while (l < r) {
        m = (l+r) >> 1;
        if (a[m] <= x) l = m + 1; else r = m;
    }
	if (l == 0) return 0;
	return a[l-1];
}

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

int main()
{
	int i, j, N, K, s, sz, lim, ans;
	
	N = in(), K = in();
	if (K == 0) { puts("0"); return 0; }
	
	s = 0, w = 0; while (N--) {
		A[w] = in();
		if (A[w] <= K) { s += A[w]; w++; }
	}
	if (s <= K) { printf("%d\n", s); return 0; }

	sz = w >> 1; if (sz > 9) sz = 9;
	N = sz, lim = 1 << sz, aw = 0;
	for (i = 1; i < lim; i++) {
		s = 0;
		for (j = 0; j < sz; j++) if ((i >> j) & 1) s += A[j];
		if (s == K) goto done;
		if (s < K) a[aw++] = s;
	}
	qsort(a, aw, sizeof(int), cmp);
	
	ans = a[aw-1], sz = w - N, lim = 1 << sz;
	for (i = 1; i < lim; i++) {
		s = 0;
		for (j = 0; j < sz; j++) if ((i >> j) & 1) s += A[j+N];
		if (s == K) goto done;
		if (s < K) {
			s += bsch(K-s);
			if (s > ans) ans = s;
		}
	}
	printf("%d\n", ans);
	return 0;
done: printf("%d\n", K);
	return 0;
}
0