結果

問題 No.4 おもりと天秤
ユーザー kuisibakuisiba
提出日時 2016-04-17 21:11:50
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,299 bytes
コンパイル時間 581 ms
コンパイル使用メモリ 71,336 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 02:17:35
合計ジャッジ時間 1,312 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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
0