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.
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 "#".
In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.
6 5 1 2 ##.#. .###. ###.. #...# .##.# ###..
11
2 5 1 1 ##### .....
5
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;
}

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





