結果
問題 |
No.4 おもりと天秤
|
ユーザー |
|
提出日時 | 2024-02-14 23:22:31 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 26 ms / 5,000 ms |
コード長 | 1,149 bytes |
コンパイル時間 | 810 ms |
コンパイル使用メモリ | 73,148 KB |
最終ジャッジ日時 | 2025-02-19 13:12:53 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#include <iostream> #include <algorithm> using namespace std; //動的計画法を用いた解法 int main(void) { int N; cin >> N; int W[N]; int sum = 0; for (int i = 0; i < N; i++) { cin >> W[i]; sum += W[i]; } //cout << "sum: " << sum << endl; if (sum % 2) { cout << "impossible" << endl; } else { sort(W, W + N, greater<int>()); int tar = sum / 2; bool possible[N+1][tar + 1]; for (int i = 0; i < N + 1; i++) { for (int j = 0; j < tar + 1; j++) { possible[i][j] = false; } } possible[0][0] = true; for (int i = 1; i < N + 1; i++) { for (int j = 0; j < tar + 1; j++) { if (possible[i-1][j]) { possible[i][j] = true; if (j + W[i-1] <= tar) { possible[i][j+W[i-1]] = true; } } } } if (possible[N][tar]) { cout << "possible" << endl; } else { cout << "impossible" << endl; } } return 0; }