結果

問題 No.4 おもりと天秤
ユーザー Kenichi HigashideKenichi Higashide
提出日時 2016-07-07 10:46:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 752 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 69,744 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-21 00:40:15
合計ジャッジ時間 7,305 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> w;
int weightSum = 0;
int weightSumHalf;

bool traverse(int position, int sum)
{
	sum += w[position];
	if (sum < weightSumHalf) {
		for (int i = position + 1; i < n; ++i) {
			if (traverse(i, sum)) {
				return true;
			}
		}
		return false;
	} else if (sum > weightSumHalf) {
		return false;
	} else {
		return true;
	}
}

int main(int argc, char* argv[])
{
	cin >> n;
	w.resize(n);
	for (int i = 0; i < n; ++i) {
		cin >> w[i];
		weightSum += w[i];
	}
	if (weightSum % 2 == 1) {
		cout << "impossible" << endl;
		return 0;
	}
	weightSumHalf = weightSum / 2;

	if (traverse(0, 0)) {
		cout << "possible" << endl;
	} else {
		cout << "impossible" << endl;
	}

	return 0;
}
0