結果

問題 No.1606 Stuffed Animals Keeper
ユーザー rtyurtyu
提出日時 2021-07-16 21:55:38
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 14 ms / 3,000 ms
コード長 829 bytes
コンパイル時間 660 ms
コンパイル使用メモリ 73,480 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2024-07-06 09:03:05
合計ジャッジ時間 1,813 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

vector<pair<int, int> > v;
int d[5009][5009], inf = 1000000000;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n; cin >> n;
	int m = 0, c0 = 0, c1 = 0;
	for (int i = 0; i <= n; i++) {
		int a = 2;
		if (i < n) cin >> a;
		if (a == 0) {
			c0++; m++;
		}
		else if (a == 1) c1++;
		else if (c0 || c1) {
			v.push_back(make_pair(c0 + c1, c1));
			c0 = c1 = 0;
		}
	}
	for (int i = 1; i <= m; i++)
		d[0][i] = inf;
	for (int i = 1; i <= v.size(); i++) {
		int a = v[i - 1].first, w = v[i - 1].second;
		for (int j = 0; j <= m; j++) {
			d[i][j] = d[i - 1][j];
			if (j >= a) d[i][j] = min(d[i][j], d[i - 1][j - a] + w);
		}
	}
	int ans = d[(int)v.size()][m];
	if (ans >= inf) cout << -1 << '\n';
	else cout << ans << '\n';
	return 0;
}
0