传送门:点我

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. Arch Linux root密码忘记了怎么办

    https://wiki.archlinux.org/index.php/Reset_root_password_(简体中文)https://wiki.archlinux.org/index.php/ ...

  2. Centos7 firewall开放3306端口

    目录 Centos7 firewall开放3306端口 1. 查看防火墙状态 2. 关闭防火墙firewall 3. 关闭防火墙firewall后开启 4. 开启端口 5. 重启防火墙 6. 常用命令 ...

  3. Nmap使用指南

    一.目标指定 1.CIDR标志位 192.168.1.0/24 2.指定范围 192.168.1.1-255 192.168.1-255.1(任意位置) 3.IPv6地址只能用规范的IPv6地址或主机 ...

  4. win32网络模型之重叠I/O

    网上大部分重叠I/O的基本概念都讲得很清楚,但是大多讲得不是很深入,实际用起来很多问题.这里只对完成实例的通知进行讨论,对问题进行总结. 重叠IO异步读写后,在某一时刻"完成"后会 ...

  5. PythonStudy——集合 Set

    # 空集合:不能用{},因为用来标示空字典 s = set() print(s, type(s)) # 概念:# 1.set为可变类型 - 可增可删# 2.set为去重存储 - set中不能存放重复数 ...

  6. day052 django第三天 url和视图

    一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...

  7. ssh 22端口号拒绝

    1:当scp或者ssh登录ubuntu远程服务的时候,出现:

  8. kafka原理和实践(四)spring-kafka消费者源码

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  9. ByteToByte64String、Base64StringToBytes

    public string ByteToByte64String(byte[] bytes) { return Convert.ToBase64String(bytes); } public byte ...

  10. <ROS> 机器人描述--URDF和XACRO

    文章转自 https://blog.csdn.net/sunbibei/article/details/52297524 特此鸣谢原创作者的辛勤付出 1 URDF 文件 1.1 link和joint ...