Garland

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
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5,4) and (2,3) are the pairs of adjacent bulbs that have different parity.

In the second case, one of the correct answers is 1 7 3 5 6 4 2.

给你一个序列n,把0的位置用1-n中的数填充,使得找到最少对相邻的数的奇偶性不同,问最少多少对这样的数。

我们把序列n填充完整,并不关心填的数是多少,只关心它的奇偶性。所以dp[i][j][1]代表已经有i个1,j个0,当前这位填奇数的答案,dp[i][j][0]代表已经有i个1,j个0,当前这位填偶数的答案。

有些状态是不可能存在的,我们设为inf。比如dp[0][j][1],dp[i][0][0],相对应只有i不等于0、j不等于0的时候才可能有dp[i][j][1]、dp[i][j][0]的转移。还有已经给出的数的位置不可能填与它奇偶性相反的。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <string>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
int dp[maxn][maxn][2];
int a[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    int k;
    if(n%2)
        k = n/2+1;
    else
        k = n/2;
    int t = n/2;
    for(int i=1; i<=t; i++)
        dp[0][i][1] = inf;
    for(int i=1; i<=k; i++)
        dp[i][0][0] = inf;
    for(int i=0; i<=k; i++)
    {
        for(int j=0; j<=t; j++)
        {
            if(a[i+j])
            {
                if(a[i+j]%2)
                {
                    if(i != 0)
                        dp[i][j][1] = min(dp[i-1][j][1],dp[i-1][j][0]+1);
                    dp[i][j][0] = inf;
                }
                else
                {
                    if(j != 0)
                        dp[i][j][0] = min(dp[i][j-1][0],dp[i][j-1][1]+1);
                    dp[i][j][1] = inf;
                }
            }
            else
            {
                if(i != 0)
                    dp[i][j][1] = min(dp[i-1][j][1],dp[i-1][j][0]+1);
                if(j != 0)
                    dp[i][j][0] = min(dp[i][j-1][0],dp[i][j-1][1]+1);
            }
        }
    }
    int ans = min(dp[k][t][0],dp[k][t][1]);
    printf("%d",ans);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值