結果

問題 No.4 おもりと天秤
ユーザー alpha_virginis
提出日時 2015-06-20 22:06:55
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,182 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 158,208 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-06-26 09:13:49
合計ジャッジ時間 2,383 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int N;
int w[128];
int total;

int memo[101][10001];

bool solve(int p, int sum) {

  if( memo[p][sum] != -1 ) {
    if( memo[p][sum] == 1 ) {
      return true;
    }
    else {
      return false;
    }
  }
  
  if( p >= N ) {
    if( sum == total/2 ) {
      memo[p][sum] = 1;
      return true;
    }
    else {
      memo[p][sum] = 0;
      return false;
    }
  }

  if( sum == total/2 ) {
    memo[p][sum] = 1;
    return true;
  }
  if( sum > total/2 ) {
    memo[p][sum] = 0;
    return false;
  }

  if( solve(p+1, sum+w[p]) ) {
    memo[p][sum] = 1;
    return true;
  }
  if( solve(p+1, sum) ) {
    memo[p][sum] = 1;
    return true;
  }

  memo[p][sum] = 0;
  return false;
}
  


int main() {

  bool ans;
  
  total = 0;

  for(int i = 0; i < 101; ++i) {
    for(int j = 0; j < 10001; ++j) {
      memo[i][j] = -1;
    }
  }
  
  std::cin >> N;
  for(int i = 0; i < N; ++i) {
    std::cin >> w[i];
    total += w[i]; 
  }

  if( total % 2 == 1 ) {
    ans = false;
  }
  else {
    ans = solve(0, 0);
  }

  if( ans ) {
    std::cout << "possible" << std::endl;
  }
  else {
    std::cout << "impossible" << std::endl;
  }
  
  return 0;
}
0