Codeforces Round #612 (Div. 1)A. Garland(三维DP)

Garland

Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of nn 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 nn (1≤n≤100) — the number of light bulbs on the garland.

The second line contains nn integers p1, p2, …, pn (0≤pi≤n) — the number on the ii-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.


题意:给你1-n个数的排列,有些位置上的数为0,就是让你填没有填进去的数。构成有不同奇偶校验的相邻灯泡对使其最小。

思路:dp
一维:长度
二维:偶数的个数
三维:奇偶性
初始化的条件:
1:长度为1,第一个数为0或者第一个数为偶数。那么 f[1][n/2-1][0]=0;
2:长度为1,第一个数位0或者第一个数位奇数。那么 f[1][n/2][1]=0;
递推方程:
1:当前位置填偶数:f[i][j-1][0]=min(f[i][j-1][0],f[i-1][j][k]+(k==1));
2:当前位置填奇数:f[i][j][1]=min(f[i][j][1],f[i-1][j][k]+(k==0));
结果:
min(f[n][0][0],f[n][0][1]);

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
int f[N][N][2];
int n;
int a[N];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	memset(f,0x3f,sizeof f);
	//若放的是偶数 
	if(n>1&&(a[1]%2==0||a[1]==0)) f[1][n/2-1][0]=0;
	//若放的是奇数 
	if((a[1]%2==1||a[1]==0) ) f[1][n/2][1]=0;
	for(int i=2;i<=n;i++)
	{
		for(int j=0;j<=n/2;j++)
		{
			for(int k=0;k<2;k++)
			{
				if(j>0&&(a[i]%2==0||a[i]==0))
					f[i][j-1][0]=min(f[i][j-1][0],f[i-1][j][k]+(k==1));
				if((i+j-1<n)&&(a[i]%2==1||a[i]==0)) 
					f[i][j][1]=min(f[i][j][1],f[i-1][j][k]+(k==0));
			}
		}
	 } 
	 cout<<min(f[n][0][0],f[n][0][1])<<endl;
	 return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值