結果
問題 | No.4 おもりと天秤 |
ユーザー |
![]() |
提出日時 | 2019-04-28 21:21:59 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 44 ms / 5,000 ms |
コード長 | 1,636 bytes |
コンパイル時間 | 1,723 ms |
コンパイル使用メモリ | 169,664 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-12-17 23:56:38 |
合計ジャッジ時間 | 2,862 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#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; }