結果

問題 No.4 おもりと天秤
ユーザー _hunachi_hunachi
提出日時 2021-10-11 15:10:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 747 bytes
コンパイル時間 2,936 ms
コンパイル使用メモリ 67,296 KB
実行使用メモリ 4,524 KB
最終ジャッジ日時 2023-10-13 17:12:10
合計ジャッジ時間 3,991 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    const int W_SUM_MAX = 1000000;
    int n;
    int w[101];
    bool dp[W_SUM_MAX];
    int w_sum_half = 0;
    fill(dp, dp + W_SUM_MAX, 0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> w[i];
        w_sum_half += w[i];
    }
    if (w_sum_half % 2 == 1) {
        cout << "impossible" << endl;
        return 0;
    }
    w_sum_half = w_sum_half / 2;
    for (int i = 0; i < n; i++) {
        for (int j = w_sum_half; j >= w[i]; j--) {
            dp[j] = dp[j] || dp[j - w[i]] || (j - w[i] == 0);
        }
    }
    if (dp[w_sum_half]) {
        cout << "possible" << endl;
    } else {
        cout << "impossible" << endl;
    }
    return 0;
}
0