CodeForces - 225C. Barcode(DP)
C. 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<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1005;
const int INF=0x3f3f3f3f;
int a[maxn][maxn];
int cnt[maxn][2];
int dp[maxn][maxn][2];
char c[maxn][maxn];
int main()
{int m,n,x,y;scanf(" %d %d %d %d ",&m,&n,&x,&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][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]);}}else {dp[i][j][0]=dp[i-1][j-1][0]+cnt[i][0];dp[i][j][1]=dp[i-1][j-1][1]+cnt[i][1];}}}int ans=INF;for(int i=x;i<=y;i++){ans=min(ans,dp[n-1][i][0]);ans=min(ans,dp[n-1][i][1]);}cout<<ans<<endl;
}
这个代码我调了三个小时,写出来之后!不知道为什么前边用cin输入这个题就过不了,而且每组样例都能本地AC。尴尬,求解?
CodeForces - 225C. Barcode(DP)相关推荐
- CF思维联系–CodeForces - 225C. Barcode(二路动态规划)
ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...
- Codeforces 833B 题解(DP+线段树)
题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...
- CodeForces 864E Fire dp递推
CodeForces 864E 题意:有 n 个物品着火,每个物品要花 ti 时间扑灭,且在 >= di 时间后就会坏掉,物品价值为 pi . 问最多可以救回多少价值,物品个数,及救哪些物品(要 ...
- Codeforces 864E - Fire(dp)
原题连接:http://codeforces.com/problemset/problem/864/E 题意:一个人想从大火中带走一些东西.每次他只能带一个,耗时ti ,价值为pi, 当总时间超过di ...
- CodeForces - 1497D Genius(dp)
题目链接:点击查看 题目大意:给出 nnn 个问题,每个问题有如下属性: tagtagtag:标签 ccc:困难度 sss:奖励值 初始时 ci=2ic_i=2^ici=2i,初始时 IQ=0IQ= ...
- C. Barcode dp
https://codeforces.com/problemset/problem/225/C 这个题目和之前一个题目很像 https://www.cnblogs.com/EchoZQN/p/1090 ...
- D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...
- Codeforces 480D Parcels(dp)
题目链接:Codeforces 480D Parcels 题目大意:就是有一个用来堆放货物的板,承重力为S.现在有N件货物,每件货物有到达的时间,运走的时间,以及 重量,承重,存放盈利.如果这件货物能 ...
- Codeforces 711c 简单dp
题目:http://codeforces.com/problemset/problem/711/C 题意: 有n棵树,m(1-m)种颜色,要求划分成k组,每组是连续的同一种颜色的树. 刚开始树有的已经 ...
最新文章
- 大规模图训练调优指南
- Sybase identity 字段
- html 并集选择器,CSS并集选择器
- Mysql数据库存储原理
- 浮点型的三个特殊值 Double.NEGATIVE_INFINITY Double.POSITIVE_INFINITY Double.NaN
- Java队列Disruptor 的使用
- 加载mySQL数据到内存_【测试验证】数据库加载到内存占用大小
- 使用sklearn PCA主成分分析对图像特征进行降维
- 大学数学实验习题--统计推断 (附答案)
- JAVA中读写文件操作
- sin_cos_tan_cot_sec_cosec 三角函数计算器
- 多种隐藏滚动条但是依然可以滚动实现方式
- matlab编写扫雷,MATLAB版本的扫雷小游戏
- 用Python编写斐波那契数列(Fibonacci Sequence)
- 计算机word画铁路,利用WORD画地图
- 事件查看器出现Netlogon5723错误
- android 图片底部波浪线,Android实现波浪线效果(xml bitmap)
- docxtpl 学习笔记
- 魔兽争霸3的MapHack制作教程(二)去除战争迷雾
- CommandArgument属性 绑定参数
热门文章
- Android开发之自定义控件的基本介绍(附源码)
- java小票_Java编程打印购物小票实现代码
- 担忧医生因AI技术而失业?杞人忧天
- SSAS系列——【08】多维数据(程序展现Cube)
- (全部)2008重磅出击——微软Windows Server 2008实战攻略系列
- ASP.Net中怎样获得存储过程传出的参数。
- spring security只要熟悉每个filter的作用和顺序
- Subversion 1.5 安装配置指南
- android中sp的意义_两分钟理解Android中SP与DP的区别
- matlab 比较日期,[转载][Matlab]关于时间的函数的不完全总结