結果
| 問題 |
No.4 おもりと天秤
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-04-17 21:11:50 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,299 bytes |
| コンパイル時間 | 581 ms |
| コンパイル使用メモリ | 70,632 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-04 10:29:54 |
| 合計ジャッジ時間 | 1,309 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 WA * 10 |
ソースコード
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
vector<int> vec(n);
int sum = 0;
for (auto& i : vec) {
cin >> i;
sum += i;
}
sort(vec.begin(), vec.end(), greater<int>());
if (sum % 2 == 1 || vec.at(0) > sum / 2) {
cout << "impossible" << endl;
return 0;
}
bool flag = false; //possibleならtrue
vector<int> y(sum + 1, 0);
y.at(0) = 1; //check済
stack<int> s;
auto itr = vec.begin();
int weight = 0;
while (true) {
s.push(*itr);
weight += s.top();
s.pop();
if (y.at(weight) == 0) {
if (weight == sum / 2) {
flag = true;
break;
} else if (weight < sum / 2) {
y.at(weight) = 1; //check済
++itr;
} else {
y.at(weight) = 1; //check済
weight -= static_cast<int>(*itr);
++itr;
}
}
if (itr == vec.end()) break;
}
cout << ((flag) ? "possible" : "impossible") << endl;
return 0;
}
// 3 2 1 , sum=6
// 5 4 3 2 , sum=14
// 2 3 5 9 10 11 12 sum=52