結果

問題 No.4 おもりと天秤
ユーザー fantasiabaetica
提出日時 2018-06-28 20:48:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 848 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 64,832 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-30 23:32:51
合計ジャッジ時間 1,297 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    int weight[n];
    int sum_weight = 0;
    for (int i = 0; i < n; i++){
        cin >> weight[i];
        sum_weight += weight[i];
    }
    
    if (sum_weight % 2 == 1){
        cout << "impossible" << endl;
        return 0;
    }
    int half_weight = sum_weight / 2;

    // 重さjを作れるか
    int dp[half_weight + 1];
    memset(dp, 0, sizeof(dp));
    int dp_tmp[half_weight + 1];
    dp[0] = 1;
    for (int i = 0; i < n; i++){
        memset(dp_tmp, 0, sizeof(dp));
        int w = weight[i];
        for (int j = half_weight; j >= w; j--){
            dp[j] |= dp[j - w];
        }
    }
    if (dp[half_weight] == 1){
        cout << "possible" << endl;
    } else {
        cout << "impossible" << endl;
    }
    return 0;
}
0