結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tnakao0123tnakao0123
提出日時 2021-07-20 17:40:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 3,000 ms
コード長 1,555 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 92,076 KB
実行使用メモリ 35,620 KB
最終ジャッジ日時 2023-09-24 12:50:43
合計ジャッジ時間 3,208 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 9 ms
20,936 KB
testcase_04 AC 11 ms
22,976 KB
testcase_05 AC 5 ms
10,612 KB
testcase_06 AC 7 ms
14,624 KB
testcase_07 AC 9 ms
18,856 KB
testcase_08 AC 8 ms
18,820 KB
testcase_09 AC 6 ms
12,500 KB
testcase_10 AC 4 ms
10,300 KB
testcase_11 AC 3 ms
8,164 KB
testcase_12 AC 6 ms
14,608 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 13 ms
25,188 KB
testcase_17 AC 13 ms
25,312 KB
testcase_18 AC 14 ms
25,276 KB
testcase_19 AC 13 ms
25,200 KB
testcase_20 AC 13 ms
25,212 KB
testcase_21 AC 5 ms
7,112 KB
testcase_22 AC 4 ms
7,144 KB
testcase_23 AC 5 ms
7,072 KB
testcase_24 AC 4 ms
7,056 KB
testcase_25 AC 5 ms
7,124 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,532 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 15 ms
35,620 KB
testcase_44 AC 15 ms
35,360 KB
testcase_45 AC 15 ms
35,440 KB
testcase_46 AC 15 ms
35,356 KB
testcase_47 AC 15 ms
35,444 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1606.cc:  No.1606 Stuffed Animals Keeper - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 5000;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int as[MAX_N], cs[2], ls[MAX_N], lcs[MAX_N][2];
int dp[MAX_N + 1][MAX_N + 1];

/* subroutines */

inline void setmin(int &a, int b) { if (a > b) a = b; }

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int m = 0;
  for (int i = 0; i < n;) {
    while (i < n && as[i] == 2) i++;
    if (i >= n) break;

    int j = i;
    while (j < n && as[j] != 2) {
      cs[as[j]]++;
      lcs[m][as[j]]++;
      j++;
    }

    ls[m] = j - i;
    m++;
    i = j;
  }
  //for (int i = 0; i < m; i++)
  //printf("%d(%d,%d) ", ls[i], lcs[i][0], lcs[i][1]); putchar('\n');

  for (int i = 0; i <= m; i++) fill(dp[i], dp[i] + cs[0] + 1, INF);
  dp[0][0] = 0;

  for (int i = 0; i < m; i++)
    for (int j = 0; j <= cs[0]; j++)
      if (dp[i][j] < INF) {
	// -> 0
	if (j + ls[i] <= cs[0])
	  setmin(dp[i + 1][j + ls[i]], dp[i][j] + lcs[i][1]);

	// -> 1
	setmin(dp[i + 1][j], dp[i][j] + lcs[i][0]);
      }

  printf("%d\n", (dp[m][cs[0]] < INF) ? dp[m][cs[0]] / 2 : -1);

  return 0;
}
0