結果

問題 No.4 おもりと天秤
ユーザー DadarithmDadarithm
提出日時 2015-09-14 23:56:35
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,079 bytes
コンパイル時間 792 ms
コンパイル使用メモリ 83,120 KB
実行使用メモリ 7,324 KB
最終ジャッジ日時 2023-09-26 11:58:50
合計ジャッジ時間 1,889 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 17 ms
6,740 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 18 ms
7,016 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 15 ms
6,552 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 WA -
testcase_18 AC 18 ms
7,324 KB
testcase_19 AC 12 ms
6,048 KB
testcase_20 AC 16 ms
6,696 KB
testcase_21 AC 13 ms
6,120 KB
testcase_22 AC 13 ms
6,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

int N;
vector<int> arr;
vector<unordered_map<int, bool>> memo;
bool ans;

bool solve(int n, int left)
{
	if (n == arr.size())
		return !left;
	else if (left < 0)
		return false;
	else
	{
		if (memo[n].find(left) != memo[n].end())
			return memo[n][left];
		else
		{
			if (solve(n + 1, left))
			{
				memo[n][left] = true;
				return true;
			}
			else
				memo[n][left] = false;
		}

		if (memo[n].find(left - arr[n]) != memo[n].end())
			return memo[n][left - arr[n]];
		else
		{
			if (solve(n + 1, left - arr[n]))
			{
				memo[n][left - arr[n]] = true;
				return true;
			}
			else
				memo[n][left - arr[n]] = false;
		}

		return false;
	}
}

int main()
{
	ans = false;
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		int j;
		cin >> j;
		arr.push_back(j);
	}
	memo.resize(N);

	int sum = 0, nsum = 0;
	for (int i = 0; i < arr.size(); ++i)
		sum += arr[i];
	if (!(sum % 2))
		ans = solve(0, sum / 2);

	cout << (ans ? "possible" : "impossible") << endl;

	return 0;
}
0