結果

問題 No.4 おもりと天秤
ユーザー seaesaeseaesae
提出日時 2018-11-24 22:50:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,125 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 52,476 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-26 05:02:23
合計ジャッジ時間 1,704 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//yukicoderNo.4(おもりと天秤)
#include<iostream>
using namespace std;

const int INF=-100000000;
int N,sum=0;
int W[110];
//dp[i][j]:i番目までのおもりを見たとき、その合計がjになるかどうか
//左から見てN番目までに重さが全体の合計値の半分になればよい
//重さが全体の合計値の半分にすることができるなら、残りを右の天秤にのせればよいからである
//1+2+3=6→dp[3][3]=true
//1+2+3+4+5→合計値がそもそも奇数なので×
bool dp[110][10010];

int main(){
    
    cin>>N;
    for(int i=0; i<N; i++){
        cin>>W[i];
        sum+=W[i];
    }
    if(sum%2==1){
        cout<<"impossible"<<endl;
        return 0;
    }
    //dp初期条件:dp[0][0]=true
    dp[0][0]=true;
    for(int i=0; i<N; i++){
        for(int j=0; j<=sum/2; j++){
            //dp漸化式:dp[i+1][j]=dp[i][j] | dp[i][j-W[i]](もらうDP)
            if(j-W[i]>=0)dp[i+1][j]=dp[i][j] | dp[i][j-W[i]];
            else dp[i+1][j]=dp[i][j];
        }
    }
    if(dp[N][sum/2])cout<<"possible"<<endl;
    else cout<<"impossible"<<endl;
    return 0;
}
0