【Codeforces 225C】Barcode
【链接】 我是链接,点我呀:)
【题意】
让你把每一列都染成一样的颜色
要求连续相同颜色的列的长度都大于等于x小于等于y
问你最少的染色次数
【题解】
先求出每一列染成#或者.需要染色多少次
设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());
}
}
}
【Codeforces 225C】Barcode的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- ZOJ 2397:Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing Time Limit: 5 Seconds Memory Limit: 32768 KB Here is a famous story ...
- @注解与普通web.xml的关系
@WebServlet(name = "SimpleServlet" ,urlPatterns = {"/simple"}) public class Simp ...
- 【转载】[Oracle] Linux下手动创建数据库过程
今天发现一个Oracle测试库的字符集设置不正确,原本的字符集是UTF-8,正确的字符集应该是ZHS16GBK,因为UTF-8是ZHS16GBK的超集,无法修改,只能重建数据库,幸好该测试库上还没有数 ...
- Speed Limit
http://poj.org/problem?id=2017 #include<stdio.h> int main() { int n,mile,hour; ) { ,h = ; whil ...
- esp和ebp指针
gdb调试的时候会出现esp和ebp这两个指针,而这两个指针为我们查看栈的情况提供了方便. 简单点说,esp指向栈顶,而ebp指向栈底.例如一段程序: #include <stdio.h> ...
- BZOJ 4481
思路: 等比数列求和 (无穷项) +线段树找逆序对 //By SiriusRen #include <bits/stdc++.h> ; ; ],ans; struct Node{int x ...
- vb.net实现textbox控件输入指定位数小数方法实现。
Private Sub textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPres ...
- js基础---数据类型转换
js中数据类型: 简单数据类型: number:233,-34,0x23,023 string:"hello"或者'hello' boolean:true.false undefi ...
- PHP常用的一些函数:
背景:这一次是对一些函数进行整理,方便以后的使用. 1.date(); date()函数的作用是获取当前日期时间,由于PHP 5.0对date()函数进行了重写,因此,当前的日期时间函数比系统时间少了 ...
- [Windows Server 2012] 手工破解MySQL密码
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:破解MySQL ...