結果
問題 |
No.4 おもりと天秤
|
ユーザー |
![]() |
提出日時 | 2018-11-24 22:50:45 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,125 bytes |
コンパイル時間 | 736 ms |
コンパイル使用メモリ | 55,216 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-24 19:28:01 |
合計ジャッジ時間 | 1,483 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
//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; }