题目链接

贪心模拟了半天,最后放弃了

题意

给你一串从1−n1-n1−n的序列,其中部分未知(表示为0),补全序列使得相邻数值奇偶性相反的数量最少

相邻数值的奇偶性相反:两个相邻的两个数值,其中一个为奇数另外一个为偶数

分析

一开始用了贪心,结果卡在第十二个样例,然后改成dp

定义dp数组如下

int dp[120][60][2];
// dp[i][j][0/1] 表示第i+1个位置放了偶/奇数,且到第i+1处总共放了j个奇数,有多少个奇偶性相反

得到状态转移方程

dp[i][j][1] = min(dp[i - 1][j - 1][0] + 1, dp[i - 1][j - 1][1]);
dp[i][j][0] = min(dp[i - 1][j][1] + 1, dp[i - 1][j][0]);

当然这得看这个位置本身是不是已经有了数值,如果为0则两个都需要,如果已经有数值了就按照原来的数值进行dp

AC代码

#include <bits/stdc++.h>

using namespace std;

void solve() {
int n;
int dp[120][60][2], value[120];
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> value[i];
}
memset(dp, 0x3f, sizeof(dp));
if (value[0] == 0)
dp[0][1][1] = dp[0][0][0] = 0;
else
dp[0][value[0] & 1][value[0] & 1] = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j <= min(i + 1, (n + 1) / 2); ++j) {
if ((value[i] & 1 || value[i] == 0) && j > 0)
dp[i][j][1] = min(dp[i - 1][j - 1][0] + 1, dp[i - 1][j - 1][1]);
if (!(value[i] & 1))
dp[i][j][0] = min(dp[i - 1][j][1] + 1, dp[i - 1][j][0]);
}
}
cout << min(dp[n - 1][(n + 1) / 2][1], dp[n - 1][(n + 1) / 2][0]) << endl;
} int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long long test_index_for_debug = 1;
char acm_local_for_debug;
while (cin >> acm_local_for_debug) {
cin.putback(acm_local_for_debug);
if (test_index_for_debug > 20) {
throw runtime_error("Check the stdin!!!");
}
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
}
#else
solve();
#endif
return 0;
}

【codeforces】Codeforces Round #612 (Div. 2) C. Garland——DP的更多相关文章

  1. 【Codeforces】CF Round #592 (Div. 2) - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...

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

    第四次写题解,请多指教! http://codeforces.com/contest/1287/problem/C题目链接 题目大意是有一个数字串挂有1-n n个数字,现在上面缺失了一些数字,让你找出 ...

  3. 【二分】CF Round #587 (Div. 3)E2 Numerical Sequence (hard version)

    题目大意 有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...,即重复的1~1,1~2....1~n,给你一个\(k\),求第\(k(k<=10^{1 ...

  4. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  5. 【题解】Codeforces 961G Partitions

    [题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...

  6. 【HDU3530】 [Sdoi2014]数数 (AC自动机+数位DP)

    3530: [Sdoi2014]数数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 682  Solved: 364 Description 我们称一 ...

  7. Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. 【Codeforces】Codeforces Round #551 (Div. 2)

    Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...

随机推荐

  1. 码海拾遗:简述C++(一)

    C++是Bjarne Stroustrup博士于1982年,在C语言的基础上引入并扩充了面向对象的概念后发明的一种新的程序语言.就与C语言的渊源而言,C++可以说是C语言的超集,它兼容C的一切(可能是 ...

  2. 阿里云https免费证书配置-包教会

      阿里云https免费证书配置-包教会-有需要请联系小编! 小编个人站点:https://www.itdog.site/ 小编微信号:wvqusrtg  

  3. Nginx之常用基本配置(二)

    上一篇我们把nginx的主配置文件结构大概介绍了下,全局配置段比较常用的指令说了一下,http配置段关于http服务器配置指令介绍了下,以及有几个调优的指令,server_name的匹配机制,错误页面 ...

  4. C++走向远洋——64(项目三、数组类模板)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. MongoDB监控及报警

    转载请注明出处:https://www.cnblogs.com/shining5/p/11142357.html MongoDB监控及报警 Prometheus是由SoundCloud开发的开源监控报 ...

  6. python爬虫-scrapy日志

    1.scrapy日志介绍 Scrapy的日志系统是实现了对python内置的日志的封装 scrapy也使用python日志级别分类 logging.CRITICAL logging.ERROE log ...

  7. 上周 GitHub 热点速览 vol.09:手撕 LeetCode 一日 star 破两千

    作者:HelloGitHub-小鱼干 摘要(用于 公众号/博客园等地方):上周 GitHub 趋势榜相较上上周就如同前故事一般,跌到不行,无论是新晋开源小项,还是坚挺老项目,Star 增长量都不如之前 ...

  8. LeetCode---二叉树3-总结例题

    二叉树-总结例题 1-从中序与后序遍历序列构造二叉树 给定二叉树的后序遍历和二叉树的中序遍历 想法: 先根据后序遍历的最后一个元素构造根节点 寻找根节点在中序遍历中的位置 递归构建根节点的左右子树 / ...

  9. 7-29 jmu-python-不同进制数 (10 分)

    输入一个十进制整数,输出其对应的八进制数和十六进制数.要求采用print函数的格式控制进行输出,八进制数要加前缀0o,十六进制数要加前缀0x. 输入格式: 输入一个十进制整数,例如:10 输出格式: ...

  10. Eureka 注册中心看这一篇就够了

    服务注册中心是服务实现服务化管理的核心组件,类似于目录服务的作用,主要用来存储服务信息,譬如提供者 url 串.路由信息等.服务注册中心是微服务架构中最基础的设施之一. 在微服务架构流行之前,注册中心 ...