結果

問題 No.4 おもりと天秤
ユーザー wild
提出日時 2017-03-09 21:07:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 747 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 71,680 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-26 10:10:15
合計ジャッジ時間 1,741 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int n;
vector<int> w;
vector<bool> dp(10001, false);

int main(){
	int n, tmp, allw=0;
	cin >> n;
	for(int i = 0; i<n; i++){
		cin >> tmp;
		w.push_back(tmp);
		allw += tmp;
	}

	//問題
	//n/2が奇数では無理
	//n個の中でちょうど重さが全体の重さ/2となる組み合わせを探せばいい
	//動的計画法でいけそう
	//左の重さを重ねていく
	//dp[左側の重さ];
	//--jにすれば追加された数に対しての判定を行わない
	dp[0] = true;
	for(int i=0; i<n; i++) {
		for(int j=10000; j >= 0; j--) {
			if(dp[j]) dp[j+w[i]] = true;
		}
	}

	if(allw%2 || !dp[allw/2]) cout << "impossible" << endl;
	else cout << "possible" << endl;
}

	
0