传送门:点我

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 nmx 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:

.##..
.##..
.##..
.##..
.##..
.##..

题意:

给定n,m,x,y。之后输入n行m列的字符串数组,其中#表示黑色,. 表示白色。询问的是要让这个字符串数组,每[x,y]列是一个颜色,最少的更改次数。

看到题目猜一下是按列的DP,推了一下午结果鸽于发现初始状态写错了。

dp[i][0]表示第i列为.时候的最小费用
dp[i][1]表示第i列为#时候的最小费用

转移方程不难得到:

dp[i][0] = min(dp[i-j][1]+(p[i][0]-p[i-j][0]),dp[i][0]); 其中  x<= j <= y;
dp[i][1] = min(dp[i-j][0]+(p[i][1]-p[i-j][1]),dp[i][1]); 其中  x<= j <= y;

其中p为前缀和,即需要改变的次数,具体看代码。

这个方程的意思是:加上i这行,起码需要x行,最多y行,所以dp[i][k]是从dp[i-y][k]~dp[i-x][k]这一段更新上来,其中k=0或者1。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
char mp[][];
int p[][];
int dp[][];
int main(){
int n,m,x,y;
memset(p,,sizeof(p));
memset(dp,0x3f,sizeof(dp));
scanf("%d %d %d %d",&n,&m,&x,&y);
for(int i = ; i < n ; i++){
scanf("%s",mp[i]);
}
for(int i = ; i < m ; i++){
for(int j = ; j < n ;j ++){
if(mp[j][i] == '#'){
p[i+][]++;
}else if(mp[j][i] == '.'){
p[i+][]++;
}
}
}
for(int i = ; i <= m ; i ++){
p[i][]+=p[i-][];
p[i][]+=p[i-][];
}
// dp[i][0]表示第i列为.时候的最小费用
// dp[i][1]表示第i列为#时候的最小费用
dp[][] = dp[][] = ;
for(int i = ; i <= m ; i++){
for(int j = x ; j <= y && j <= i; j ++){
//加上i这行起码x行,最多y行,所以从dp[i-y]到dp[i-x]更新上来
dp[i][] = min(dp[i-j][]+(p[i][]-p[i-j][]),dp[i][]);
dp[i][] = min(dp[i-j][]+(p[i][]-p[i-j][]),dp[i][]);
}
}
printf("%d\n",min(dp[m][],dp[m][]));
}
/*
6 5 1 2
##.#.
.###.
###..
#...#
.##.#
###..
*/

CF 225C Barcode(DP)的更多相关文章

  1. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  3. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  4. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  5. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  6. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  7. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  8. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  9. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

随机推荐

  1. Javascript 2.3

    声明多个变量用逗号隔开    var teacher=30,stu=40; Javascript变量允许包含 美元符号  $

  2. mysql下载以及安装

    因为xampp怎么都连接不上mysql,我感觉有可能是因为装mysql的时候试了很多次才安装成功,之前的mysql没有卸载干净造成的,今天把mysql卸载干净,又重新安装配置环境,但是还是连接不上,然 ...

  3. solr6.4.1搜索引擎(3)增量同步mysql数据库

    尚未实现首次同步mysql数据库的,请参考我的另一篇文章http://www.cnblogs.com/zhuwenjoyce/p/6512378.html(solr6.4.1搜索引擎同步mysql数据 ...

  4. PAT 乙级 1092 最好吃的月饼 (20 分)

    1092 最好吃的月饼 (20 分) 月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出 ...

  5. Git从远程clone项目报错cannot open git-upload-pack,将http.sslVerify设为false即可

    通过HTTPS访问Git远程仓库,如果服务器的SSL证书未经过第三方机构签署,那么Git就会报错 通过https访问Git远程仓库,如果服务器的SSL证书没有经过第三方机构签署,就会出现cannot ...

  6. background-size的应用情景:当给出的背景图片大于实际网页需要布局的图片大小时

    网页需求是:50*50 如果只设置  width:50px;height:50px;background-image("images/XXX.png"); 效果如下: 添加设置:b ...

  7. 【产品设计】【转】APP界面设计规范编写指南(人人都是产品经理)

    转自 :http://www.woshipm.com/ucd/608557.html 作者:EID_UX_DESIGN,微信公众号:EID_center 原文地址:http://www.ui.cn/d ...

  8. Java内存泄漏定位

    Java虚拟机内存分为五个区域:方法区,堆,虚拟机栈,本地方法栈,程序计数器.其中方法区和堆是java虚拟机共享的内存区域,虚拟机栈,本地方法栈,程序计数器是线程私有的. 程序计数器(Program ...

  9. Java并发编程三个性质:原子性、可见性、有序性

      并发编程 并发程序要正确地执行,必须要保证其具备原子性.可见性以及有序性:只要有一个没有被保证,就有可能会导致程序运行不正确  线程不安全在编译.测试甚至上线使用时,并不一定能发现,因为受到当时的 ...

  10. IntelliJ IDEA 导入多个maven项目

    IntelliJ IDEA的功能十分强大  我们日常开发项目会分多个maven项目 idea单个打开切换很是麻烦 下边是idea可以同时导入多个项目的方法 1.选择 FIle -> NEW -& ...