Codeforces Round #139 (Div. 2) C. Barcode(DP)

37 篇文章 0 订阅
7 篇文章 0 订阅

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 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.

Sample test(s)
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:

.#.#.
.#.#.

这道题过的有一点点小奇怪,知道了状态转移方程之后,我写出的程序总是与答案差一,于是减一之,过之。。。。

题意:要求序列的每一列都变成相同的字符,且连续的#串的列数要在x与y之间,求 最少变几次。

状态转移方程:dp[i+j][0]=min(dp[i+j][0],dp[i][1]+(a[i+j]-a[i]));
                            dp[i+j][1]=min(dp[i+j][1],dp[i][0]+j*n-(a[i+j]-a[i]));

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
int n,m,x,y,i,j,k;
int dp[1005][2],a[1005];
char b[1005][1005];

int main()
{
  while(~scanf("%d%d%d%d",&n,&m,&x,&y))
   {
       memset(a,0,sizeof(a));
       memset(b,0,sizeof(b));
     for(i=0;i<n;i++)
      {
          scanf("%s",b[i]);
          for(j=0;j<m;j++)
            if(b[i][j]=='#')
               a[j+1]++;
        }

     for(i=1;i<m;i++)
        a[i+1]+=a[i];
    for(i=0;i<=m;i++)
     dp[i][0]=dp[i][1]=m*n;
    dp[0][1]=dp[0][0]=1;
    for(i=0;i<m;i++)
     {
         for(j=x;j<=y&&i+j<=m;j++)
           {
               dp[i+j][0]=min(dp[i+j][0],dp[i][1]+(a[i+j]-a[i]));
               dp[i+j][1]=min(dp[i+j][1],dp[i][0]+j*n-(a[i+j]-a[i]));
               //cout<<dp[i+j][0]<<"***"<<dp[i+j][1]<<" ";
           }
        //cout<<endl;
     }
    cout<<min(dp[m][0],dp[m][1])-1<<endl;
   }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值