結果
問題 |
No.1606 Stuffed Animals Keeper
|
ユーザー |
![]() |
提出日時 | 2021-08-23 20:51:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 42 ms / 3,000 ms |
コード長 | 1,958 bytes |
コンパイル時間 | 2,405 ms |
コンパイル使用メモリ | 201,856 KB |
最終ジャッジ日時 | 2025-01-24 01:40:15 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
#include <bits/stdc++.h> using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template <typename Element, typename Head, typename ...Args> auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template <typename Element, typename Head, typename ...Args> auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template <typename T, typename Compare = less<T>> T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); } template <typename T, typename Compare = less<T>> T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); } int main() { constexpr int inf = INT_MAX / 2; int n; cin >> n; auto a = vec<int>(uns, n); for (auto &e : a) cin >> e; auto b = [&] { auto b = vec<tuple<int, int>>(uns, 0); int last = 0; for (int i = 0; i < n; ++i) { if (a[i] != 2) { continue; } if (0 < i - last) { b.push_back({ i - last, count(begin(a) + last, begin(a) + i, 1) }); } last = i + 1; } if (0 < n - last) { b.push_back({ n - last, count(begin(a) + last, begin(a) + n, 1) }); } return b; }(); auto dp = vec<int>(inf, size(b) + 1, n + 1); dp[0][0] = 0; for (int i = 1; i <= size(b); ++i) { auto [l, c] = b[i - 1]; for (int j = 0; j <= n; ++j) { dp[i][j] = dp[i - 1][j]; if (0 <= j - l) { chmin(dp[i][j], dp[i - 1][j - l] + c); } } } auto ans = dp[size(b)][count(begin(a), end(a), 0)]; if (ans != inf) { cout << ans << endl; } else { cout << -1 << endl; } }