CF Garland(遇到的第一个野生dp)

报告首长:发现一个野生的dp!!

但是我没做出来,虽然题目并不困难(02叫我不要自大! 哇咔哒)~

Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.在这里插入图片描述

Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.

No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.

Input

The first line contains a single integer n (1≤n≤100) — the number of light bulbs on the garland.

The second line contains n integers p1, p2, …, pn (0≤pi≤n) — the number on the i-th bulb, or 0 if it was removed.

Output

Output a single number — the minimum complexity of the garland.

Examples
Input

5
0 5 0 2 3

Output

2

Input

7
1 0 0 5 0 0 2

Output

1

哈哈,还是没有做出来呢,脑子只想着怎么模拟了,真的是猪脑子哦,不过我倒是让我看到了大家的强大哈哈。

题目大意的的转化:

给你1-n中得所有数字组成的长度为n数列,其中的一些数字被擦掉,要求让你补全这些数字,并要求造成的复杂度最低(复杂度是指相邻数字奇偶性不同的个数), 也就是每个数字的空位上选择奇数或者是偶数填入其中,这就很自然的能想到选择dp来解决。

状态表示:其中f[i][j][0-1]表示前i个数字中偶数所剩的个数为j然后第i个位置上填0(偶数), 1(奇数)。

状态转移:看代码

#include <iostream>
#include <cstring>

using namespace std;

const int N = 110;

int f[N][N][2], a[N];

int main()
{
    int n; cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i]; 
    
    memset(f, 0x3f, sizeof f);
    
    if(a[1] == 0 || a[1] & 1) f[1][n / 2][1] = 0;
    if(a[1] == 0 || a[1] % 2 == 0) f[1][n / 2 - 1][0] = 0;
    
    for (int i = 2; i <= n; i ++)
    {
        for (int j = 0; j <= n / 2; j ++)
        {
            if(a[i] != 0)
            {
                if(a[i] & 1) f[i][j][1] = min(f[i - 1][j][0] + 1, f[i - 1][j][1]);
                else f[i][j - 1][0] = min(f[i - 1][j][1] + 1, f[i - 1][j][0]);
            }
            else 
            {
                f[i][j - 1][0] = min(f[i - 1][j][1] + 1, f[i - 1][j][0]);
                f[i][j][1] = min(f[i - 1][j][0] + 1, f[i - 1][j][1]);
            }
        }
    }
    cout << min(f[n][0][1],f[n][0][0]) << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值