CodeForces 225C Barcode

本文介绍了一种算法,旨在通过最少的操作将给定的黑白像素矩阵转换为条形码形式,即每列颜色一致且连续相同颜色的列宽限制在特定范围内。文章详细解释了算法思路,并提供了完整的代码实现。

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.

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:

.#.#.
.#.#.

/*  CodeForces 225C
给出一个矩阵,包含两种颜色,
问最少修改多少个点才能让每一列的符号相同,
且符号相同的连续的列的长度在x和y之间。

先算出前i列调整为全黑列的花费a[i]和调整为全白列的花费b[i],
dp[i+j][0] 表示前i列且 第i到第j列都调整为全黑的最小花费
dp[i+j][1] 表示前i列且 第i到第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] + b[i+j] - b[i])

最后答案是 min(dp[m][0],dp[m][1])
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 5;

int dp[1005][2];
int a[1005], b[1005];
int main(void)
{
//	freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
    int n, m, x, y, i, j, k;
    char c;
    cin >> n >> m >> x >> y;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(dp, INF, sizeof(dp));
    for (i = 1; i <= n; i++){
        for (j = 1; j <= m; j++){
            cin >> c;
            if (c == '#') a[j]++;
            else b[j]++;
        }
    }
    for (i = 1; i < m; i++){
        a[i+1] += a[i];
        b[i+1] += b[i];
    }
    dp[0][0] = dp[0][1] = 0;
    for (i = 0; i < m; i++){
        for (j = x; j <= y; j++){
            if (i + j <= m){
                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] + b[i + j] - b[i]);
            }
        }
    }
    cout << min(dp[m][0], dp[m][1]) << endl;

    return 0;
}



评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值