报告首长:发现一个野生的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;
}