結果

問題 No.4 おもりと天秤
ユーザー nukacha
提出日時 2017-05-19 12:30:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 715 ms / 5,000 ms
コード長 1,038 bytes
コンパイル時間 884 ms
コンパイル使用メモリ 83,680 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-06-26 10:17:20
合計ジャッジ時間 2,263 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <numeric>
#include <map>

int N;
std::vector<int> W;
std::map<std::pair<int, int>, bool> mp;

bool check(int ind, int t) {
    std::pair<int, int> p = std::make_pair(ind, t);
    if (mp.find(p) != mp.end()) {
        return mp[p];
    }
    if (t == 0) {
        mp[p] = true;
        return true;
    } else if (t < 0) {
        mp[p] = false;
        return false;
    }
    for (int i = ind + 1; i < N; i++) {
        if (check(i, t - W[i])) {
            mp[p] = true;
            return true;
        }
    }
    mp[p] = false;
    return false;
}

int main() {
    int sum;
    int target;
    std::cin >> N;
    W.resize(N);
    for (int i = 0; i < N; i++) {
        std::cin >> W[i];
    }
    sum = std::accumulate(W.begin(), W.end(), 0);
    target = sum / 2;
    if (sum % 2 == 1) {
        std::cout << "impossible" << std::endl;
    } else if (check(0, target)) {
        std::cout << "possible" << std::endl;
    } else {
        std::cout << "impossible" << std::endl;
    }
}
0