結果
問題 | No.1606 Stuffed Animals Keeper |
ユーザー |
![]() |
提出日時 | 2021-07-16 21:34:58 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 18 ms / 3,000 ms |
コード長 | 929 bytes |
コンパイル時間 | 1,805 ms |
コンパイル使用メモリ | 172,576 KB |
実行使用メモリ | 14,592 KB |
最終ジャッジ日時 | 2024-07-06 08:31:59 |
合計ジャッジ時間 | 3,283 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
#include <bits/stdc++.h>using namespace std;const int INF = 10000000;int main(){int N;cin >> N;vector<int> A(N);for (int i = 0; i < N; i++){cin >> A[i];}vector<int> cnt0(1, 0), cnt1(1, 0);int s0 = 0;int cnt = 1;for (int i = 0; i < N; i++){if (A[i] == 0){cnt0.back()++;s0++;}if (A[i] == 1){cnt1.back()++;}if (A[i] == 2){cnt0.push_back(0);cnt1.push_back(0);cnt++;}}vector<vector<int>> dp(cnt + 1, vector<int>(s0 + 1, INF));dp[0][0] = 0;for (int i = 0; i < cnt; i++){for (int j = 0; j <= s0; j++){if (j + cnt0[i] + cnt1[i] <= s0){dp[i + 1][j + cnt0[i] + cnt1[i]] = min(dp[i + 1][j + cnt0[i] + cnt1[i]], dp[i][j] + cnt1[i]);}dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);}}if (dp[cnt][s0] == INF){cout << -1 << endl;} else {cout << dp[cnt][s0] << endl;}}