結果

問題 No.15 カタログショッピング
ユーザー bal4ubal4u
提出日時 2019-04-10 21:43:50
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 2,679 bytes
コンパイル時間 1,391 ms
コンパイル使用メモリ 31,356 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2023-09-22 10:46:49
合計ジャッジ時間 2,045 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 17 ms
19,372 KB
testcase_06 AC 17 ms
19,336 KB
testcase_07 AC 18 ms
19,456 KB
testcase_08 AC 17 ms
19,440 KB
testcase_09 AC 17 ms
19,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

#if 0
#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 599999 //500009 //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[65540]; int sz;
int p[35], N;
int S;
int len1, len2;
char ans[52][35];
int  asz;

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

	k = 0;
	for (i = 0; i < len1; i++) if ((bit1 >> i) & 1) ans[asz][k++] = i + 1;
	if (se > 0) {
		for (i = 0; i < len2; i++) if ((bit2 >> i) & 1) ans[asz][k++] = 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 >> j) & 1) 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