結果

問題 No.4 おもりと天秤
ユーザー megashares
提出日時 2018-09-13 15:38:03
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 750 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 23,936 KB
実行使用メモリ 40,568 KB
最終ジャッジ日時 2024-07-01 04:25:42
合計ジャッジ時間 1,004 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
main.cpp:19:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |                 scanf("%d", &arr[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>

int f[110][100010] = {};
const int inf = -1000000;

int max(int a, int b) {
	return a > b ? a : b;
}

int main() {
	int n;
	int arr[110];
	int i, j;
	int sum = 0;

	scanf("%d", &n);

	for (i = 1; i <= n; i++) {
		scanf("%d", &arr[i]);
		sum += arr[i];
	}

	if (sum % 2 != 0) {
		printf("impossible");
	}
	else {
		sum /= 2;

		for (i = 1; i <= n; i++) {
			for (j = 0; j <= sum; j++) {
				f[i][j] = inf;
			}
		}

		f[1][arr[1]] = 1;

		for (i = 2; i <= n; i++) {
			for (j = 0; j <= sum; j++) {

				if (j - arr[i] >= 0) {
					f[i][j] = max(f[i - 1][j], f[i - 1][j - arr[i]] + 1);
				}
			}
		}

		if (f[n][sum] >= 0) {
			printf("possible");
		}
		else {
			printf("impossible");
		}

		getchar(); getchar();
		return 0;
	}
}
0