CF思维联系–CodeForces - 225C. Barcode(二路动态规划)
Desciption
You’ve got an n × m pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture.
A picture is a barcode if the following conditions are fulfilled:
All pixels in each column are of the same color.
The width of each monochrome vertical line is at least x and at most y pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than x or greater than y.
Input
The first line contains four space-separated integers n, m, x and y (1 ≤ n, m, x, y ≤ 1000; x ≤ y).
Then follow n lines, describing the original image. Each of these lines contains exactly m characters. Character “.” represents a white pixel and “#” represents a black pixel. The picture description doesn’t have any other characters besides “.” and “#”.
Output
In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.
Examples
input
6 5 1 2
##.#.
.###.
###…
#…#
.##.#
###…
output
11
input
2 5 1 1
…
output
5
Note
In the first test sample the picture after changing some colors can looks as follows:
.##…
.##…
.##…
.##…
.##…
.##…
In the second test sample the picture after changing some colors can looks as follows:
.#.#.
.#.#.
-------------------------****-------------------
先把每列修改的数量保存下来,都变成#的花费,和都变成‘ . ’的花费,这样状态就变成了第i列,就变成一个普通DP题。
为什么是二路动态规划,是因为一开始的选择有两种,虽然一开始的颜色是有大有小的,但是基于动态规划,当前最优解并不一定是全局最优,所以这一点我们把它们分成两部分。
这道题思考起来没有那没有那么复杂,讨论第i列染或不染(变成#或不变)但是无论染或不染都要至少染或不然连续的X以上,且不可超过Y列,那么就是说当换状态时如果变成另一种状态一定是从另一种状态的连续的x到Y列变化而来,而不改变状态时就是连续的一种状态,累加当前状态的消耗。进而得到状态转移方程
for(int k=x;k<=y;k++){
dp[i][j][0]=min(dp[i][j][0],dp[i-1][k][1]+cnt[i][0]);
dp[i][j][1]=min(dp[i][j][1],dp[i-1][k][0]+cnt[i][1]);
}
进而可得出代码
#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
int f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *f;
}
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
//---------------https://lunatic.blog.csdn.net/-------------------//
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
int cnt[maxn][2];
int dp[maxn][maxn][2];
int main()
{
int m, n, x, y;
read(m),read(n),read(x),read(y);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
char x;
scanf(" %c", &x);
if (x == '#')
cnt[j][1]++;
else
cnt[j][0]++;
}
}
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i < n; i++)
{
if (i == 0){
for (int k = 0; k < 2; k++)
dp[i][1][k] = cnt[i][k];
continue;
}
for (int j = 1; j <= i+1&& j <= y; j++)
{
if (j == 1)
{
for (int k = x; k <= y; k++)
{
dp[i][1][1] = min(dp[i - 1][k][0] + cnt[i][1], dp[i][1][1]);
dp[i][1][0] = min(dp[i - 1][k][1] + cnt[i][0], dp[i][1][0]);
}
}
else
{
dp[i][j][1] = dp[i - 1][j - 1][1] + cnt[i][1];
dp[i][j][0] = dp[i - 1][j - 1][0] + cnt[i][0];
}
}
}
int ans = INF;
for (int i = x; i <= y; i++)
{
ans = min(dp[n - 1][i][0], min(dp[n - 1][i][1], ans));
}
wi(ans);
P;
}
这个代码我调了三个小时,写出来之后!不知道为什么前边用cin输入这个题就过不了,而且每组样例都能本地AC。尴尬,求解?
CF思维联系–CodeForces - 225C. Barcode(二路动态规划)的更多相关文章
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- Codeforces 225C Barcode(矩阵上DP)
题目链接:http://codeforces.com/contest/225/problem/C 题目大意: 给出一个矩阵,只有两种字符'.'和'#',问最少修改多少个点才能让每一列的字符一致,且字符 ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- CF思维联系– Codeforces-987C - Three displays ( 动态规划)
ACM思维题训练集合 It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in ...
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)
ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CodeForces 225C Barcode DP
也是一道dp ,想到了就会觉得很巧妙 矩阵中只有白块和黑块,要求repaint后满足下述条件: 每列一种颜色 根据输入范围x, y 要求条纹宽度在[x, y] 之间 数据范围: n, m, x and ...
随机推荐
- MySQL入门,第六部分,关系代数
关系代数是一种集合操作为基础过程化查询语言,特点:运算对象是关系,运算结果亦为关系 一.关系代数的特点 运算对象:关系 运算结果:关系 运算符:四类 集合运算符 专门的关系运算符 算术比较符 逻辑运算 ...
- 同步工具类—— CountDownLatch
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 CountDownLatch简介 CountDownLa ...
- go1.13errors的用法
go1.13errors的用法 前言 基本用法 fmt.Errorf Unwrap errors.Is As 扩展 参考 go1.13errors的用法 前言 go 1.13发布了error的一些新的 ...
- win10+ubuntu双系统修复ubuntu启动引导
因为windows是不能引导linux的,而每次win10升级或恢复都会将linux的启动引导覆盖掉,导致无法进入linux, 所以一直就禁止了win10更新.这几天win10出了点小毛病,所以就狠下 ...
- C# 基础知识系列- 10 反射和泛型(二)
0. 前言 这篇文章延续<C# 基础知识系列- 5 反射和泛型>,继续介绍C#在反射所开发的功能和做的努力.上一篇文章大概介绍了一下泛型和反射的一些基本内容,主要是通过获取对象的类型,然后 ...
- 分屏神器PoweToys
win+~调用设置分屏界面,shift+软件拖到分屏位置
- Application.Exit
Application.Exit:通知winform消息循环退出.Environment.Exit:终止当前进程,返回exitcode给操作系统 Application.Exit会在所有前台线程退出后 ...
- week homework: 大家来找茬
上周课程主题为用户体验,每位同学也根据自己使用APP的体验,例举出一些手机或电脑客户端软件的bug或用户体验非常不好的地方: Tianfu: GitHub.com:界面不够直观,有许多功能不知道入口在 ...
- Spark SQL源码解析(二)Antlr4解析Sql并生成树
Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 这一次要开始真正介绍Spark解析SQL的流程,首先是从Sql Parse阶段开始,简单点说, ...
- 从零开始学习docker之docker的安装
一.Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 OverlayFS 类的 Union FS 等技术,对进程 ...