結果

問題 No.4 おもりと天秤
ユーザー mh72012246mh72012246
提出日時 2016-10-24 15:05:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,224 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 71,808 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 17:12:29
合計ジャッジ時間 1,755 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <sstream>   // istringstream
#include <algorithm> // sort
#include <utility>   // pair
#include <cfloat>    // DBL_MAX

typedef long long ll;
using namespace std;

//// search all
// bool rec(vector<int> vec, int idx, int N, int tgt){
//   if(idx >= N || tgt < 0){ return false; }
//   if(vec[idx] == tgt) { return true; }
//   return rec(vec, idx+1, N, tgt-vec[idx])
//     || rec(vec, idx+1, N, tgt);
// }

int main(){
  int N;
  cin >> N;

  vector<int> weights (N);
  int sum=0;
  for(int i=0; i<N; i++){
    cin >> weights[i];
    sum += weights[i];
  }

  // odd
  if(sum%2){
    cout << "impossible" << endl;
    return 0;
  }

  vector<bool> able (sum/2+1,false);
  able[0] = true;

  //// main
  // sort(weights.begin(), weights.end());
  int maxId = 0;
  for(int i=0; i<N; i++){ // weights
    for(int j=maxId; j>=0; j--){ // list
      if(able[j] && j+weights[i] <= sum/2){
        able[j+weights[i]] = true;
        maxId = max(maxId, j+weights[i]);
      }
    }
  }

  if(able[sum/2]){
    cout << "possible" << endl;
  } else {
    cout << "impossible" << endl;
  }

  // if(sum%2 || !rec(weights, 0, N, sum/2)){

  return 0;
}
0