Codeforces Round #612 (Div. 2) C. Garland

27 篇文章 0 订阅

Codeforces Round #612 (Div. 2) C. 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.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5I6fVwhx-1578535853294)(https://espresso.codeforces.com/c87ff8b17badad0f05d83d783c8343a0a00725a6.png)]

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 2 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 1 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 ) n (1≤n≤100) n(1n100) — the number of light bulbs on the garland.

The second line contains n integers p 1 , p 2 , … , p n ( 0 ≤ p i ≤ n ) p1, p2, …, pn (0≤pi≤n) p1,p2,,pn(0pin) — the number on the $ i-th $ bulb, or 0 0 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.

思路:动态规划

一维:长度,

二维:剩余偶数个数

三维:当前数奇偶性

界限:

  • 当存在偶数且第一个数为0或偶数: d p [ 1 ] [ n / 2 − 1 ] [ 0 ] = 0 dp[1][n/2-1][0] = 0 dp[1][n/21][0]=0
  • 当第一个数为 00 或奇数: d p [ 1 ] [ n / 2 ] [ 1 ] = 0 dp[1][n/2][1] = 0 dp[1][n/2][1]=0

状态转移:

  • 当前填偶数: d p [ i ] [ j − 1 ] [ 0 ] = m i n ( d p [ i ] [ j − 1 ] [ 0 ] , d p [ i − 1 ] [ j ] [ k ] + ( k = = 1 ) ) dp[i][j-1][0] = min(dp[i][j-1][0], dp[i-1][j][k] + (k==1)) dp[i][j1][0]=min(dp[i][j1][0],dp[i1][j][k]+(k==1))
  • 当前填奇数: d p [ i ] [ j ] [ 0 ] = m i n ( d p [ i ] [ j ] [ 0 ] , d p [ i − 1 ] [ j ] [ k ] + ( k = = 0 ) ) dp[i][j][0] = min(dp[i][j][0], dp[i-1][j][k] + (k==0)) dp[i][j][0]=min(dp[i][j][0],dp[i1][j][k]+(k==0))

AC:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, a[maxn], dp[maxn][maxn][2];
int main(){
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    memset(dp, 0x3f, sizeof(dp));
    if(n > 1 && (a[1] == 0 || a[1] % 2 == 0))
        dp[1][n / 2 - 1][0] = 0;
    if(a[1] % 2 == 1 || a[1] == 0)
        dp[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] == 0 || a[i]%2 == 0))
                    dp[i][j - 1][0] = min(dp[i][j - 1][0], dp[i - 1][j][k] + (k == 1));
                if(i+j-1 < n && (a[i] == 0 || a[i] % 2 == 1))
                    dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][k] + (k == 0));
            }
        }
    }
    cout << min(dp[n][0][0], dp[n][0][1]) << endl;
    return 0;
}
EduSoho是杭州阔知网络科技有限公司推出的开源、免费的互联网教育产品,帮助企业、个人快速建立互联网教育网站。本开源软件简洁易用,定制性强,是开展互联网教学、销售、推广的最好的选择。 更新公告: 6.6.12(2015-10-22) 修复:修复编辑器的文件上传时,MIME-TYPE的识别错误 6.6.11 (2015-10-15) 修复:注册验证码的浏览器兼容问题 6.6.10 (2015-10-13) 修复:邮件服务器设置过期错误。 6.6.9 (2015-10-13) 优化:课程支持彻底删除; 优化:后台用户管理列表添加注册时间和登录时间查询,用户表支持查询结果导出(需用户导入导出插件); 优化:试卷添加防复制功能; 优化:图片上传压缩优化。 修复:会员到期提醒时间显示错误。 修复:班级介绍页中签到无法点击的错误 6.6.8 (2015-10-08) 优化:云短信验证码安全性强化; 优化:适配新版移动端APP。 修复:求知主题下云视频白屏。 6.6.7(2015-10-05) 修复:修复某些浏览器中观看云视频时出现白屏现象 6.6.6(2015-09-27) 修复:修复嵌入第三方视频无法观看的问题 6.6.5(2015-09-25) 优化:重构播放器的代码结构 优化:提升日志安全级别; 优化:优化手机APP登入二维码时效性; 修复:修复作业的解析显示问题; 6.6.4 (2015-09-22) 优化:教师列表页样式调整; 优化:课程与班级添加三级分类; 优化:实名认证管理添加检索功能; 优化:后台课程管理中添加收费情况筛选条件; 优化:新增微信内APP下载跳转页面; 优化:简墨主题首页海报编辑区图片文案优化。 修复:后台添加用户时用户名长度限制错误; 修复:班级课程免费课时预览页面班级购买提示; 修复:微信内分享登录后手机登录绑定原有账号的问题。 6.6.3 (2015-09-10) 优化:微信扫码支付 6.6.2 (2015-09-10) 修复:PHP版本兼容问题 6.6.1 (2015-09-10) 修复:移动端接口问题 6.6.0 (2015-09-08) 新增:手机APP下载页开放全新版安卓版阔知学堂3.0,后台增新旧版本切换选项; 新增:新增班级展示开关和购买开关,封闭班级重新定义,原封闭班级选项移除。 优化:侧边栏的二维码交互效果; 优化:手机APP下载页的交互效果; 优化:实名认证审核列表排序。 修复:导航通知小红点错误显示的问题; 修复:实名认证通过后信息可修改的问题; 修复:班级课程老师作为未购买用户预览课程时页面报错的问题 修复:在邮箱注册模式下批量导入用户绕过了强制邮箱验证的问题。 6.5.5(2015-09-01)  优化:EduSoho APP端接口调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值