結果

問題 No.15 カタログショッピング
ユーザー bal4ubal4u
提出日時 2019-04-10 21:28:48
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 2,689 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 31,540 KB
実行使用メモリ 17,104 KB
最終ジャッジ日時 2023-09-22 09:56:21
合計ジャッジ時間 1,233 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 15 ms
17,004 KB
testcase_06 AC 15 ms
16,968 KB
testcase_07 AC 14 ms
17,024 KB
testcase_08 AC 14 ms
17,104 KB
testcase_09 AC 14 ms
16,980 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:28:17: 備考: in expansion of macro ‘pc’
   28 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.15 カタログショッピング
// 2019.4.10 bal4u

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

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

void out(int n)  // 非負整数の表示(出力)
{
	int i;
	char b[20];

	if (!n) pc('0');
	else {
		//		if (n < 0) pc('-'), n = -n;
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
}

//// ハッシュ関数
#define HASHSIZ 300007
typedef struct { int n, id; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash + HASHSIZ;

int lookup(int n)
{
	HASH *p = hash + n % HASHSIZ;
	while (p->n) {
		if (p->n == n) return p->id;
		if (++p == hashend) p = hash;
	}
	return -1;
}

int insert(int n, int id)
{
	HASH *p = hash + n % HASHSIZ;
	while (p->n) {
		if (p->n == n) return p->id;
		if (++p == hashend) p = hash;
	}
	p->n = n, p->id = id;
	return -1;
}

///// 本問題関連
int tbl[65540][50], hi[65536]; int sz;
int p[35], N;
int S;
int len1, len2;
char ans[52][35]; int alen[52];
int  asz;

void pr(int fi, int bit1, int se, int bit2)
{
	int i;

	for (i = 0; i < len1; i++) if (bit1 & (1 << i)) ans[asz][alen[asz]++] = i + 1;
	if (se > 0) {
		for (i = 0; i < len2; i++) if (bit2 & (1 << i)) {
			ans[asz][alen[asz]++] = se + i + 1;
		}
	}
	asz++;
}

void calc(int fr, int k, int mode)
{
	int i, j, s, id, lim;

	lim = 1 << k;
	for (i = 1; i < lim; i++) {
		s = 0;
		for (j = 0; j < k; j++) if (i & (1<<j)) s += p[fr + j];
		if (s > S) continue;
		if (mode == 0) {         // ハッシュテーブルに登録
			id = insert(s, sz);
			if (id < 0) id = sz++;
			tbl[id][hi[id]++] = i;
		}
		else if (mode == 1) {    // ハッシュテーブルに問い合わせる
			if (S == s) pr(0, i, -1, 0);
			else {
				if ((id = lookup(S - s)) >= 0) {
					for (j = 0; j < hi[id]; j++) pr(0, i, N / 2, tbl[id][j]);
				}
			}
		}
		else {                   // そのまま表示
			if (S == s) pr(0, i, -1, 0);
		}
	}
}

int cmp(const void *a, const void *b) { return memcmp((char *)a, (char *)b, 35); }
int main()
{
	int i, j, f, n;

	n = in(), S = in();
	N = 0; for (i = 0; i < n; i++) {
		p[N] = in(); if (p[N] <= S) N++;
	}
	if (N > 4) {
		n = N >> 1;
		len1 = n, len2 = N - n;
		calc(N >> 1, len2, 0);
		calc(0, len1, 1);
	} else calc(0, len1 = N, 2);
	qsort(ans, asz, 35, cmp);
	for (i = 0; i < asz; i++) {
		f = 0;
		for (j = 0; ans[i][j] > 0; j++) {
			if (f) pc(' ');
			else f = 1;
			out(ans[i][j]);
		}
		pc('\n');
	}
	return 0;
}
0