【Codeforces 225C Barcode】+ 动态转移

47 篇文章 3 订阅
43 篇文章 0 订阅

Barcode
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status

Description

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.

Sample Input
Input

6 5 1 2

.#.

.###.

..

.##.#

..

Output

11

Input

2 5 1 1

#

…..

Output

5

Hint

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:

.#.#.
.#.#.

先统计每列的黑白图形的个数,然后就进行DP了~~感觉不是很难吧~~

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int n,m,x,y,w[MAXN],b[MAXN],dp[2][MAXN];
char st[MAXN];
int main()
{
    scanf("%d %d %d %d",&n,&m,&x,&y);
    fill(w + 1,w + 1 + m,0);
    fill(b + 1,b + 1 + m,0);
    memset(dp,1,sizeof(dp));
    for(int i = 1 ; i <= n ; i++){
        scanf("%s",st + 1);
        for(int j = 1 ; j <= m ; j++)
            if(st[j] == '#') w[j]++;
            else b[j]++;
    }
    for(int i = 1 ; i <= m ; i++) w[i] += w[i-1],b[i] += b[i - 1];
    dp[0][0] = dp[1][0] = 0;
    for(int i = 0 ; i < m ; i++)
        for(int j = x ; j <= y ; j++){ // 列的宽度
            dp[0][i + j] = min(dp[0][i + j],dp[1][i] + w[i + j] - w[i]);
            dp[1][i + j] = min(dp[1][i + j],dp[0][i] + b[i + j] - b[i]);
        }
    printf("%d\n",min(dp[0][m],dp[1][m]));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值