【codeforces】Codeforces Round #612 (Div. 2) C. Garland——DP
题目链接
贪心模拟了半天,最后放弃了
题意
给你一串从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的更多相关文章
- 【Codeforces】CF Round #592 (Div. 2) - 题解
Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...
- Codeforces Round #612 (Div. 2)C. Garland
第四次写题解,请多指教! http://codeforces.com/contest/1287/problem/C题目链接 题目大意是有一个数字串挂有1-n n个数字,现在上面缺失了一些数字,让你找出 ...
- 【二分】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 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- 【题解】Codeforces 961G Partitions
[题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...
- 【HDU3530】 [Sdoi2014]数数 (AC自动机+数位DP)
3530: [Sdoi2014]数数 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 682 Solved: 364 Description 我们称一 ...
- 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 ...
- 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 ...
- 【Codeforces】Codeforces Round #551 (Div. 2)
Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...
随机推荐
- React Native拆包及热更新方案 · Solartisan
作者:solart 版权声明:本文图文为博主原创,转载请注明出处. 随着 React Native 的不断发展完善,越来越多的公司选择使用 React Native 替代 iOS/Android 进行 ...
- android 中webview的屏幕适配问题
两行代码解决WebView的屏幕适配问题 一个简单的方法,让网页快速适应手机屏幕,代码如下 1 2 WebSettings webSettings= webView.getSettings(); we ...
- 人工智能VS投资者:股票市场上谁的胜算更高?
人工智能研究历史渊源,当人工智能与资本投资,尤其是股票投资相结合或许听起来有些异想天开,但正如科幻作家William Gibson所言:"未来已经到来,只是分布不均." 在股票市场 ...
- 正则表达式之RegExp对象
1.定义 RegExp对象是原生JavaScript中表示正则表达式的对象:是正则表达式(regular expression)的简写.有一点需要明确知道,正则表达式只能对字符串进行操作. 2.语法 ...
- Google Hacking --你真的会用Google吗?
你真的会用Google吗?Google Hacking提升工作效率 阅读本文需要6.66分钟 Google hacking,也叫作google dorking.如果在 Google 上搜索 Googl ...
- 7-46 jmu-python-求单词长度 (10 分)
输入n个单词,计算每个单词长度.对单词长度排序,分行输出单词长度及其单词. 输入格式: 行1:单词个数n 分行输入n个单词 输出格式: 分行输出单词长度及其单词.(单词长度,单词)用元组表示 输入样例 ...
- 前端每日实战:125# 视频演示如何用纯 CSS 创作一个失落的人独自行走的动画
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MqpOdR/ 可交互视频 此视频是 ...
- GZOJ 1361. 国王游戏【NOIP2012提高组DAY1】
国王游戏[NOIP2012提高组DAY1] Time Limit:1000MS Memory Limit:128000K Description 国王游戏(game.cpp/c/pas) [问题描述] ...
- Error response:/usr/bin/tf_serving_entrypoint.sh: line 3: 6 Illegal instruction (core dumped) ...
用docker部署tensorflow-serving:gpu时,参照官方文档:https://tensorflow.google.cn/tfx/serving/docker 本应该是很简单的部署,没 ...
- Mac笔记本使用小道解答集
如何设置Mac默认应用程序 https://www.jianshu.com/p/0f912e6c846c 苹果本安装微软雅黑 下载微软雅黑字体Mac版 解压.ttf 拖拽放入 我的电脑/资源库/fon ...