結果

問題 No.4 おもりと天秤
ユーザー drymousedrymouse
提出日時 2024-02-14 23:06:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,003 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 74,944 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-14 23:06:05
合計ジャッジ時間 2,019 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

//動的計画法を用いた解法
int main(void) {
    int N;
    cin >> N;
    int W[N];
    int sum = 0;
    for (int i = 0; i < N; i++) {
        cin >> W[i];
        sum += W[i];
    }
    //cout << "sum: " << sum << endl;
    if (sum % 2) {
        cout << "impossible" << endl;
    } else {
        sort(W, W + N, greater<int>());
        int tar = sum / 2;
        bool possible[N+1][sum / 2 + 1] {false};
        possible[0][0] = true;
        for (int i = 1; i < N; i++) {
            for (int j = 0; j < tar + 1; j++) {
                if (possible[i-1][j]) {
                    possible[i][j] = true;
                    if (j + W[i-1] <= tar) {
                        possible[i][j+W[i-1]] = true;
                    }
                }
            }
        }
        if (possible[N][tar]) {
            cout << "possible" << endl;
        } else {
            cout << "impossible" << endl;
        }
    }
    return 0;
}
0