結果
問題 | No.1606 Stuffed Animals Keeper |
ユーザー |
![]() |
提出日時 | 2021-07-16 22:22:07 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 26 ms / 3,000 ms |
コード長 | 1,174 bytes |
コンパイル時間 | 1,929 ms |
コンパイル使用メモリ | 200,900 KB |
最終ジャッジ日時 | 2025-01-23 02:13:13 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (auto &&x : a) { cin >> x; } a.push_back(2); vector<int> len, s; int k = 0, t = 0; for (auto &&x : a) { if (x == 2) { len.push_back(k); s.push_back(t); k = 0; t = 0; } else { k++; t += x; } } int m = len.size(), total = count(a.begin(), a.end(), 1); constexpr int inf = 1e9; vector dp(m + 1, vector<int>(total + 1, inf)); dp.at(0).at(0) = 0; for (int i = 0; i < m; i++) { for (int j = 0; j <= total; j++) { dp.at(i + 1).at(j) = min(dp.at(i + 1).at(j), dp.at(i).at(j) + s.at(i)); if (j + len.at(i) <= total) { dp.at(i + 1).at(j + len.at(i)) = min(dp.at(i + 1).at(j + len.at(i)), dp.at(i).at(j) + len.at(i) - s.at(i)); } } } if (dp.at(m).at(total) == inf) { cout << -1 << endl; } else { cout << dp.at(m).at(total) / 2 << endl; } return 0; }