CodeForces 225C Barcode
2016-08-07 23:10
183 查看
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
output
input
output
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:
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:
.#.#. .#.#.
/* 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; }
相关文章推荐
- Codeforces 225C Barcode【dp】
- codeforces 225C Barcode
- codeforces 225C Barcode (dp)
- 【Codeforces 225C Barcode】+ 动态转移
- Codeforces-225C:Barcode(DP)
- codeforces 225C C. Barcode(dp)
- CodeForces 225C Barcode DP
- Barcode条形码及对图片进行270翻转
- CodeForces 22B Bargaining Table 简单DP
- Codeforces 629C Famil Door and Brackets
- CodeForces 510B B. Fox And Two Dots
- Codeforces 779B-Weird Rounding
- Cipher CodeForces - 156C(dp)
- codeforces 126B Password - Z-Box算法
- [Codeforces] 79
- CodeForces 128B String 后缀数组 或 优先队列维护 求第K小子串
- codeforces 630KIndivisibility(容斥原理)
- codeforces 628 B. New Skateboard (数学-被4整除)
- 【Codeforces-402C】-Dishonest Sellers(思维)
- 【CodeForces】598B - Queries on a String