結果

問題 No.4 おもりと天秤
ユーザー arrowsarrows
提出日時 2017-01-20 02:38:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 763 bytes
コンパイル時間 1,927 ms
コンパイル使用メモリ 165,796 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 17:17:41
合計ジャッジ時間 4,017 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define MAX_N 100
#define MAX_S 5000

int N,W[MAX_N],sum;

void solve(){
  bool dp[MAX_N + 1][MAX_S + 1];
  memset(dp,false,sizeof(dp));
  if(sum & 1){
    cout << "impossible" << endl;
    return;
  }
  dp[0][0] = true;
  for(int i = 0 ; i < N ; i++){
    for(int j = i ; j >= 0 ; j--){
      for(int k = MAX_S-W[i] ; k >= 0 ; k--){
        if(dp[j][k]){
          dp[j+1][k+W[i]] = true;
        }
      }
    }
  }
  bool found = false;
  for(int i = 1 ; i < N-1 ; i++){
    if(dp[i][sum/2]){
      found = true;
      break;
    }
  }
  cout << (found ? "possible" : "impossible") << endl;
}

int main(){
  cin >> N;
  for(int i = 0 ; i < N ; i++){
    cin >> W[i];
    sum += W[i];
  }
  solve();
  return 0;
}
0