CF 225C Barcode(DP)
传送门:点我
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
6 5 1 2
##.#.
.###.
###..
#...#
.##.#
###..
11
2 5 1 1
#####
.....
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)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
随机推荐
- 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf
========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...
- 用python 实现一个栈
前言 Python本身已有顺序表(List.Tupple)的实现,所以这里从栈开始. 什么是栈 想象一摞被堆起来的书,这就是栈.这堆书的特点是,最后被堆进去的书,永远在最上面.从这堆书里面取一本书出来 ...
- 1.2.4 Excel快速建立n个文件夹
1.准备员工信息表,选中名字 2.[设置单元格格式]>[数字]>[自定义]>右侧的[类型]>输入”md ”@>单击[确定] 3.确定后在姓名前会出现md,新建文本文档,将 ...
- sed用法说明
sed介绍 sed:stream editor 是一个行编辑器,或叫流编辑器,每次处理一行,处理完一行再处理下一行.sed并不直接处理源文件,而是读取一行后放入模式空间(patten space)里, ...
- Android 动态注册JNI函数
1.JNI函数注册方式 在Android开发中,由于种种原因我们需要调用C/C++代码,在这个时候我们就需要使用jni了, jni在使用时要对定义的函数进行注册,这样java才能通过native关键字 ...
- .NET 定时执行任务解决方案(Timer & Quartz.Net)
共有两种方法: 一.使用Timer global.asax <%@ Application Language="C#" %> <%@ import Namespa ...
- Centos6.5部署Sonar6.7.1备注
1.一定要用非root账号登录(自己建立账号),建立Sonar目录并部署,因为使用了Elasticsearch 5.6.3做搜索服务器,而它不允许用root账号启动服务,会报如下错误: Excepti ...
- 浏览器调试动态js脚本
前两天拉取公司前端代码修改,发现在开发者工具的sources选项里边,居然没有列出来我要调试的js脚本,后来观察了一下,脚本是动态在页面里引入的,可能是因为这样所以不显示出来,但是如果不能断点调试,只 ...
- 软件-集成开发环境:IDEA(Java 语言开发的集成环境)
ylbtech-软件-集成开发环境:IDEA(Java 语言开发的集成环境) IDEA 全称IntelliJ IDEA,是用于java语言开发的集成环境(也可用于其他开发语言),IntelliJ在业界 ...
- Net-Snmp工具(学习SNMP的工具,开源项目)简单使用
https://blog.csdn.net/mrzhangzifu/article/details/77882371 Net-Snmp工具的安装与配置 操作系统:Ubuntu16.4 软件版本:n ...