Codeforces 225C Barcode【dp】

C. Barcode
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

.#.#.
.#.#.

题目大意:

给你一个N*M的矩阵,其中有白色和黑色两种颜色,我们可以将其中的任意格子颜色变成另外一种颜色,问最少操作多少次(变换颜色),使得这个N*M的矩阵变成一个条形码,并且保证相同颜色挨在一起的列数不小于x不大于y、


思路:


1、首先预处理出每一列变成一种颜色需要多少花费,记做a【i】【2】,其中a【i】【0】表示将第i列都变成点的花费,a【i】【1】表示将第i列都变成#的花费。


2、那么考虑dp过程:

①设定dp【i】【j】【k(0/1)】表示现在进行到第i列上,当前列颜色为k(0/1),并且其前边的列与当前列颜色相同的连续列数为j的最小花费。

②那么不难推出其状态转移方程:
if(j==1)dp【i】【j】【k】=min(dp【i-1】【l】【k】,dp【i】【j】【k】+a【i】【k】)【x<=l<=y】

表示当前列的颜色要与上一列的颜色不同,那么其要满足上一列的颜色连续的列数在x到y之间,在这个区间内,维护最小值。

if(j!=1)dp【i】【j】【k】=dp【i-1】【j-1】【k】+a【i】【k】;

表示当前列的颜色要与上一列的颜色相同,那么直接从上一列的状态转移过来即可。

③时间复杂度O(m^2)


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char map[1005][1005];
int dp[1005][1005][2];
int a[1005][2];
int main()
{
    int n,m,x,y;
    while(~scanf("%d%d%d%d",&n,&m,&x,&y))
    {
        memset(dp,0x3f3f3f3f,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
        }
        //a[j][0] 都弄成#
        //a[j][1] 都弄成.
        for(int j=0;j<m;j++)
        {
            int cnt=0;
            for(int i=0;i<n;i++)
            {
                if(map[i][j]=='#')cnt++;
            }
            a[j][0]=cnt;
            a[j][1]=n-cnt;
        }
        for(int i=0;i<m;i++)
        {
            if(i==0)
            {
                dp[i][1][0]=a[i][0];
                dp[i][1][1]=a[i][1];
                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]+a[i][0]);
                        dp[i][j][1]=min(dp[i][j][1],dp[i-1][k][0]+a[i][1]);
                    }
                }
                else
                {
                    dp[i][j][0]=dp[i-1][j-1][0]+a[i][0];
                    dp[i][j][1]=dp[i-1][j-1][1]+a[i][1];
                }
            }
        }
        int output=0x3f3f3f3f;
        for(int i=x;i<=y;i++)
        {
            output=min(dp[m-1][i][0],output);
            output=min(dp[m-1][i][1],output);
        }
        printf("%d\n",output);
    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值