結果

問題 No.4 おもりと天秤
ユーザー Masanari KIMURAMasanari KIMURA
提出日時 2016-09-22 01:06:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,510 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 171,480 KB
実行使用メモリ 6,984 KB
最終ジャッジ日時 2023-09-08 17:11:26
合計ジャッジ時間 2,876 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

//#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// 基本テンプレート
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// 型
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// 定数
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion


int n;
vector<int> w;
vector< vi > dp;
int half;

bool rec(int i, int weight) {
    if(dp[i][weight] != -1) return dp[i][weight];
    if(i==n) return weight==half;

    return dp[i][weight] = rec(i+1, weight+w[i]) || rec(i+1, weight);
}

int main() {
    cin >>n;
    int sum = 0;
    w.assign(n, 0);
    rep(i, n) {
        int a;
        cin >> a;
        w[i] = a;
        sum += w[i];
    }
    if(sum%2!=0) {
        P("impossible");
        return 0;
    }
    dp.assign(n+1, vector<int>(sum+1, -1));
    half = sum / 2;
    P(rec(0,0) ? "possible" : "impossible");
    return 0;
}
0