結果

問題 No.4 おもりと天秤
ユーザー tokkakatokkaka
提出日時 2016-07-16 22:09:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 939 bytes
コンパイル時間 822 ms
コンパイル使用メモリ 91,064 KB
実行使用メモリ 7,512 KB
最終ジャッジ日時 2023-09-08 16:47:11
合計ジャッジ時間 1,785 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <functional>
#include <tuple>
#include <climits>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>

using namespace std;

std::vector<int> weights;

std::vector<std::vector<int>> dp;
bool solve(int index, int left, int right)
{
    if(dp[index][left] != -1)
        return dp[index][left]==1;
    if(index == weights.size()) {
        if(left == right) return true;
        return false;
    }
    dp[index][left] = (solve(index+1, left+weights[index], right) || solve(index+1, left, right+weights[index]))?1:0;
    return dp[index][left]==1;
}

int main()
{
    int N;
    cin >> N;
    weights.resize(N);
    for(auto &weight : weights) cin >> weight;
    dp.resize(N+1);
    for(int i=0; i<N+1; i++)
        dp[i].resize(100*100, -1);
    cout << (solve(0, 0, 0)?"possible":"impossible") << endl;
}
0