結果

問題 No.4 おもりと天秤
ユーザー C_kumoC_kumo
提出日時 2019-01-19 01:10:20
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 1,372 bytes
コンパイル時間 907 ms
コンパイル使用メモリ 29,116 KB
実行使用メモリ 6,040 KB
最終ジャッジ日時 2023-09-21 12:15:46
合計ジャッジ時間 7,646 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,004 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define POS "possible"
#define IMP "impossible"

void iniUse(char n,char use[]);
char searchSum(char n,char w[],char use[],short sum,short tmpS);

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

int main(void)
{
	char n,i;
	char *w,*use;
	char ret;
	short sum;

	scanf("%hhd",&n);
	w = (char *)malloc(sizeof(char) * n);
	sum = 0;
	for (i=0;i<n;i++) {
		scanf("%hhd",&w[i]);
		sum += w[i];
	}
	
	// check even
	if (sum % 2 == 1) {
		printf("%s\n",IMP);
		return 0;
	}

	sum /= 2;

	// sort to ascending
	qsort(w,n,sizeof(char),compare);

	// check large
	if (w[n-1] > sum) {
		printf("%s\n",IMP);
		return 0;
	}

	// search
	use = (char *)malloc(sizeof(char) * n);
	iniUse(n,use);
	ret = searchSum(n,w,use,sum,0);

	if (ret == 1) {
		printf("%s\n",POS);
	} else printf("%s\n",IMP);

	return 0;
}

void iniUse(char n,char use[])
{
	char i;
	for (i=0;i<n;i++) use[i] = 0;
	return;
}

char searchSum(char n,char w[],char use[],short sum,short tmpS)
{
	char i;
	char ret;
	short tmp;

/*
	printf("use= ");
	for (i=0;i<n;i++) printf("%hhd ",use[i]);
	printf("\n");
*/
	for (i=n-1;i>=0;i--) {
		if (use[i] == 1) continue;

		tmp = tmpS + w[i];
		if (tmp > sum) continue;
		use[i] = 1;
		if (tmp == sum) return 1;

		ret = searchSum(n,w,use,sum,tmp);
		if (ret == 1) return 1;
		use[i] = 0;
	}

	return 0;
}
0