結果

問題 No.4 おもりと天秤
ユーザー komori3komori3
提出日時 2017-08-24 23:29:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,574 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 71,520 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 17:33:52
合計ジャッジ時間 1,476 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <stack>
#include <utility>
#include <queue>
//#include <random>
#include <time.h>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define SWAP(a,b) (a ^= b,b = a ^ b,a ^= b)
#define swap(type,a,b) do{type _c;_c=a;a=b;b=_c;}while(0)

using namespace std;
typedef long long int lint;

template <typename _Ty>
ostream& operator << (ostream& ostr, const vector<_Ty>& v) {
	if (v.empty()) {
		cout << "{ }";
		return ostr;
	}
	cout << "{" << v.front();
	for (auto itr = ++v.begin(); itr != v.end(); itr++) {
		cout << ", " << *itr;
	}
	cout << "}";
	return ostr;
}

int yuki0004()
{
	int N, tmp, sum = 0;
	cin >> N;
	vector<int> W(N + 1, 0);

	for (int i = 1; i <= N; i++) {
		cin >> tmp;
		W[i] = tmp;
		sum += tmp;
	}

	if (sum % 2 == 1) {
		cout << "impossible" << endl;
		return 0;
	}

	sum /= 2;

	vector < vector < bool > > dp(N + 1, vector<bool>(sum + 1, false));

	dp[0][0] = 1;
	for (int i = 1; i <= N; i++) {
		for (int j = 0; j <= sum; j++) {
			if (dp[i - 1][j]) {
				if (j + W[i] == sum) {
					cout << "possible" << endl;
					return 0;
				}

				dp[i][j] = true;
				if (j + W[i] < sum) {
					dp[i][j + W[i]] = true;
				}
			}
		}
	}

	cout << "impossible" << endl;
	return 0;
}


int main()
{
	//clock_t start, end;
	//start = clock();

	yuki0004();

	//end = clock();
	//printf("%d msec.\n", end - start);

	return 0;
}
0