結果

問題 No.4 おもりと天秤
ユーザー @abcde@abcde
提出日時 2019-04-28 21:21:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 1,636 bytes
コンパイル時間 1,665 ms
コンパイル使用メモリ 156,360 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-22 21:38:18
合計ジャッジ時間 3,165 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 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 38 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 41 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,384 KB
testcase_10 AC 45 ms
4,388 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 19 ms
4,384 KB
testcase_19 AC 35 ms
4,380 KB
testcase_20 AC 36 ms
4,384 KB
testcase_21 AC 33 ms
4,380 KB
testcase_22 AC 35 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// 天秤で計測可能な重さの合計を更新する.
// @param m: 重さの合計のリスト.
// @param w: 分銅.
// @return ret: 更新した重さの合計のリスト.
map<int, int> updateBalance(map<int, int> m, int w){
    map<int, int> ret;
    ret[w]++;
    for(auto &p : m) ret[p.first], ret[p.first + w]++;
    // for(auto &p : ret) cout << "updateBalance(ret): " << p.first << " ";
    // cout << endl;
    return ret;
}

int main() {
    
    // 1. 入力情報取得.
    int N;
    cin >> N;
    int W[N];
    cin >> W[0];
    int total = W[0];
    for(int i = 1; i < N; i++){
        cin >> W[i];
        total += W[i];
    }
    
    // 2. 重さ合計が奇数なら, 存在しないので, 終了.
    if(total % 2 != 0){
        cout << "impossible" << endl;
        return 0;
    }
    
    // 3. 天秤で計測可能な重さの合計を更新していく.
    int half = total / 2;
    map<int, int> m;
    m[W[0]]++;
    for(int i = 1; i < N; i++){
        map<int, int> lm = updateBalance(m, W[i]);
        swap(m, lm);
    }
    // cout << "half=" << half << endl;
    // for(auto &p : m) cout << p.first << " ";
    // cout << endl;
    
    // 4. 出力 ~ 後処理.
    // ex.
    // 30
    // 1 2 3 5 7 7 8 9 9 9 11 11 11 12 13 15 16 16 17 18 5 55 23 1 24 3 4 7 8 20
    // total=350, half=175 で, 
    // left:  1 1 3 3 4 5 5 7 7 7 8 8 9 9 9 11 11 11 12 13 15 16
    // right: 2, 16, 17, 18, 20, 23, 24, 55
    // -> possible と思われる.
    string ans = (m[half] > 0) ? "possible" : "impossible";
    cout << ans << endl;
    return 0;
    
}
0