結果

問題 No.4 おもりと天秤
ユーザー kkslucy1
提出日時 2018-03-08 22:11:52
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 699 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 166,360 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-06-26 10:31:38
合計ジャッジ時間 2,467 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int main() {
	int n, half;
	int w[101];
	cin >> n;
	half = 0;
	for (int i = 1;i <= 100;i++) {
		w[i] = 0;
	}
	for (int i = 0;i < n;i++) {
		int a;
		cin >> a;
		w[a]++;
		half += a;
	}
	if (half % 2 != 0) {
		cout << "impossible" << endl;
		return 0;
	}
	half /= 2;
	set<int> s[101];
	s[100].insert(half);
	for (int i = 100;i >= 1;i--) {
		for (int j = 0;j <= w[i];j++) {
			for (set<int>::iterator it = s[i].begin();it != s[i].end();it++) {
				if (*it - i * j==0) {
					cout << "possible" << endl;
					return 0;
				}
				else if (*it - i * j > 0) {
					s[i - 1].insert(*it - i * j);
				}
			}
		}
	}
	cout << "impossible" << endl;
	return 0;
}
0