【Codeforces 225C】Barcode
【链接】 我是链接,点我呀:)
【题意】
【题解】
先求出每一列染成#或者.需要染色多少次 设f[0][i][j]表示前i列,以i为结尾的连续列长度为j的#列最少需要染色多少次 设f[1][i][j]表示前i列,以i为结尾的连续列长度为j的.列最少需要染色多少次 f[0][i][j]可以由f[0][i-1][j-1]转移过来 但是j==1的时候比较特殊 会由f[1][i-1][x..y]转移过来(由里面的最小值转移) 为了方便 所以在算f[1][i-1][x..y]的时候 可以把这些的最小值存在f[0][i][0]里面 这样就不用每次转移j的时候都重新求最小值了(不过临时求问题也不大) 最后取min(f[0][m][x..y],f[1][m][x..y])【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)1e3;
static class Task{
String []s;
int cost[][];
int f[][][];
public void solve(InputReader in,PrintWriter out) {
s = new String[N+10];
cost = new int[2][N+10];
f = new int[2][N+10][N+10];
int n,m,x,y;
n = in.nextInt();m = in.nextInt();x = in.nextInt();y = in.nextInt();
for (int i = 0;i <= n-1;i++) s[i] = in.next();
for (int i = 0;i < n;i++)
for (int j = 0;j < m;j++) {
char key = s[i].charAt(j);
if (key=='#')
cost[0][j+1]++;
}
for (int j = 1;j <= m;j++) cost[1][j] = n-cost[0][j];
for (int p = 0;p < 2;p++)
for (int i = 0;i <= N;i++)
for (int j = 0;j <= N;j++)
f[p][i][j] = (int)1e7;
f[0][1][1] = cost[0][1];
if (x<=1 && 1<=y) f[1][1][0] = cost[0][1];
f[1][1][1] = cost[1][1];
if (x<=1 && 1<=y) f[0][1][0] = cost[1][1];
for (int i = 2;i <= m;i++)
for (int j = 1;j <= y;j++){
f[0][i][j] = Math.min(f[0][i][j], f[0][i-1][j-1]+cost[0][i]);
if (x<=j && j<=y) f[1][i][0] = Math.min(f[1][i][0], f[0][i][j]);
f[1][i][j] = Math.min(f[1][i][j], f[1][i-1][j-1]+cost[1][i]);
if (x<=j && j<=y) f[0][i][0] = Math.min(f[0][i][0], f[1][i][j]);
//f[0][i][1] = min{f[1][i-1][1],f[1][i-1][2],f[1][i-1][3]...f[1][i-1][y]
//f[1][i][1] = min(f[0][i-1][1],f[0][i-1][2],f[0][i-1][3]...f[0][i-1][y]
}
int ans = (int)1e7;
for (int i = x;i <= y;i++) {
ans = Math.min(ans, f[1][m][i]);
ans = Math.min(ans, f[0][m][i]);
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}