【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- windowActionModeOverlay
windowActionModeOverlay: android:windowActionModeOverlay=“true|false” : actionmode 弹出时覆盖部分布局 若 ...
- linux 网络编程-基础篇01
#Socket简介 是一个编程接口是一种特殊的文件描述符(everything in Unix is a file)并不仅限于TCPIP协议面向连接(Transmission Control Prot ...
- Java 数组的 12 个方法
1. 声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...
- 如何根据configure.ac和Makefile.am为开源代码产生当前平台的Makefile
1 2 3 4 5 6 7 8 9 //根据configure.in和Makefile.am生成makefile的步骤,基于UBUNTU 12.04 1.autoscan (可选) 2.aclocal ...
- java线程系列---Runnable和Thread的区别 (转载)
转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...
- shopnc学习
---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...
- JS判断数组是否包含某元素
我在学习ES6数组拓展时,发现了新增了不少了有趣的数组方法,突然想好工作中判断数组是否包含某个元素是非常常见的操作,那么这篇文章顺便做个整理. 1.for循环结合break 可能很多人第一会想到for ...
- 一种高兼容性的JavaBean序列化方案
在对JavaBean做序列化时,我们可能在某些场景希望前后兼容性好一些.比如所有的javaBean都序列化后保存在数据库,用的时候需要反序列化创建.随着业务的发展,数据模型可能会进行变更,那么原来的数 ...
- azkaban-executor启动时出现conf/global.properties (No such file or directory)的问题解决(图文详解)
问题详情 // :: INFO [FlowRunnerManager] [Azkaban] Cleaning recently finished // :: INFO [FlowRunnerMana ...
- C#模拟百度登录并到指定网站评论回帖(二)
序言: 回归正题:前面讲到的抓包分析的数据,是模拟登录要获得得必要信息(当然有些也不是必要的...我只是都列举出来这样有个对比)如果说,有哪个英文字母不知道什么意思的,可以问一下度娘,有不少前辈都发过 ...