Codeforces Round #612 (Div. 1) - BenFromHRBUST

博客讲述了如何使用动态规划解决一个关于恢复序列的问题,其中序列的复杂度定义为相邻元素奇偶性不同的对数。作者最初误以为是思维题,经过多次尝试和调整代码,最终发现是动态规划问题,并给出了相应的解决方案。代码实现包括状态转移方程和优化后的算法复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces Round #612 (Div. 1)

比赛传送门

A - Garland

Problem Description

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.

题意

有一个1-n的原序列,现在挖掉了几个数,尝试恢复这个序列,使得相邻数的奇偶性不同的数最少。

思路

我一定要写一下这题!!!
太难受了!!!
原本以为是一道思维题,疯狂调细节,疯狂WA,从60行代码调到200行代码,然后又删了重新调,一直没搞出来。
后来才知道是DP。。。

定义dp[i][j][k].
i 表示当前位;j 表示1至当前位有多少偶数;k 中0表示当前位为偶数,1表示当前位为奇数。
如果当前位是偶数,有dp方程:dp[i][j][0] = min(dp[i-1][j-1][0], dp[i-1][j-1][1]+1)
如果当前位是奇数,有dp方程:dp[i][j][1] = min(dp[i-1][j][0]+1, dp[i-1][j][1])
如果当前位是0,则二者都有可能,都转移即可。
最后min(dp[n][n/2][0], dp[n][n/2][1])即是答案。

这么一看,这尼玛就是一道水题啊!!!
思维黑洞是也。

Code

#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;

#define rep(i,a,b) for(int i = (a); i <= (b); i ++)
#define per(i,a,b) for(int i = (a); i >= (b); i --)
#define mkp(a,b) make_pair(a,b)

//#define __int128 ll

const int N = 2e5 + 5;
const int M = 100 + 5;
const int INF = 0x3f3f3f3f;
//const double EPS = 1e-9;
//const double PI = acos(-1);
//const double PI = 3.14;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
//const int dx[] = {-1, 1, 0, 0};
//const int dy[] = {0, 0, -1, 1};

int arr[M];
int dp[M][M][2];

int main()
{
    int n;
    scanf("%d", &n);
    rep(i,1,n) scanf("%d", &arr[i]);
    memset(dp, 0x3f3f3f3f, sizeof(dp));
    dp[0][0][0] = 0;
    dp[0][0][1] = 0;
    rep(i,1,n)
    {
        rep(j,0,i)
        {
            if(arr[i]==0)
            {
                if(j>0)
                    dp[i][j][0] = min(dp[i-1][j-1][0], dp[i-1][j-1][1]+1);
                dp[i][j][1] = min(dp[i-1][j][0]+1, dp[i-1][j][1]);
            }
            else if(arr[i]%2)
            {
                dp[i][j][1] = min(dp[i-1][j][0]+1, dp[i-1][j][1]);
            }
            else if(j>0)
            {
                dp[i][j][0] = min(dp[i-1][j-1][0], dp[i-1][j-1][1]+1);
            }
        }
    }
    printf("%d\n", min(dp[n][n/2][0], dp[n][n/2][1]));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值