結果

問題 No.4 おもりと天秤
ユーザー hanorverhanorver
提出日時 2016-05-18 10:12:42
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 868 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 64,860 KB
実行使用メモリ 817,616 KB
最終ジャッジ日時 2024-04-15 22:53:10
合計ジャッジ時間 3,008 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 MLE -
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<iostream>
#include<vector>
#include<algorithm>
#include<numeric>

int main() {
	int n;
	std::cin >> n;

	std::vector<int> weight(n);
	for (int i = 0; i < n; i++) {
		std::cin >> weight[i];
	}

	int sum = std::accumulate(weight.begin(), weight.end(), 0);

	// 重さが奇数では天秤が平らになることはない
	if (sum % 2 == 1) {
		std::cout << "impossible" << std::endl;
		return 0;
	}

	sum /= 2; // この重さを作れたら勝ち

	std::vector<int> dp(1, 0);

	for (int i = 0; i < weight.size(); i++) {
		for (int j = dp.size() - 1; j >= 0; j--) {
			if (dp[j] + weight[i] == sum) {
				std::cout << "possible" << std::endl;
				return 0;
			}
			if (dp[j] + weight[i] > sum) continue;
			dp.push_back(dp[j] + weight[i]);
		}
		dp.erase(std::unique(dp.begin(), dp.end()), dp.end());
	}

	std::cout << "impossible" << std::endl;


	return 0;
}
0