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 ...
随机推荐
- json的fromjson的方法使用。可以在volley中进行使用
Gson提供了fromJson()方法来实现从Json相关对象到Java实体的方法. 在日常应用中,我们一般都会碰到两种情况,转成单一实体对象和转换成对象列表或者其他结构. 先来看第一种: 比如jso ...
- java web知识点复习,重新编写学生选课系统的先关操作。
为了复习之前学习的相关的html,javaweb等知识.自己有重新编写了一遍学生选课系统. 下面主要展示登录界面的代码,以及各个大的主页面的相关jsp. <%@ page language=&q ...
- C++ stringstream(强制类型转换)
#include <iostream>#include <string>#include <sstream>using namespace std;int main ...
- Linux bash篇(三 数据流重定向)
1> 以覆盖的方式将正确的数据输出到文件或设备上 1>> 以追加的方式将正确的数据输出到文件或设备上 2> 以覆盖的方式将错误的数据输 ...
- Javascript 入门 document
JS可以在hmtl中直接插入文本. <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- JAVA中Calendar 类的应用
转自:https://www.imooc.com/code/2340 侵删! Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法 ...
- Mysql中的分库分表
mysql中的分库分表分库:减少并发问题分表:降低了分布式事务分表 1.垂直分表 把其中的不常用的基础信息提取出来,放到一个表中通过id进行关联.降低表的大小来控制性能,但是这种方式没有解决高数据量带 ...
- Linux C++ 网络编程学习系列(4)——多路IO之epoll基础
epoll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll 源码说明: server.cpp: 监听127.1:6666,功 ...
- 内存对齐 align
/* 地址对齐:指的是存放数据的首地址%N==0,而且整个结构体的大小%M(结构体的有效对齐值)==0 1 数据类型的自身对齐值:char:1 short:2 int,flolat,double:4 ...
- 【图解】你还在为 TCP 重传、滑动窗口、流量控制、拥塞控制发愁吗?看完图解就不愁了
每日一句英语学习,每天进步一点点: 前言 前一篇「硬不硬你说了算!近 40 张图解被问千百遍的 TCP 三次握手和四次挥手面试题」得到了很多读者的认可,在此特别感谢你们的认可,大家都暖暖的. 来了,今 ...