【链接】 我是链接,点我呀:)

【题意】

让你把每一列都染成一样的颜色
要求连续相同颜色的列的长度都大于等于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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. balsamiq mockups 注册

    Name: helloWorld Key: eJzzzU/OLi0odswsqslIzcnJD88vykmpsUQCNc41hjV+7q5+AF74Ds8=

  2. [linux环境配置]个人用持续更新ing~

    alias ll='ls -la' export PATH=$PATH:~/Desktop/myscript alias gpush='git push origin HEAD:refs/for/ma ...

  3. NodeJs函数式编程

    虽然标题是NodeJS函数式编程,但实际上NodeJS 是一个框架,不是一种语言,其采用的语言是 JavaScript.而JavaScript是一种典型的多范式编程语言,算不上是函数式语言,但它有函数 ...

  4. form表单点击后验证

    function check(){ var customertype = document.getElementById("customertype"); //alert(cust ...

  5. jeecg中列表查询数据关联其他表的显示

    1.A表字段:id,name;B表字段:id,name,fid(A表外键),现查询A表和B表的所有数据并且查询条件A,B都有,在前台页面list显示 2.后台方法: @RequestMapping(p ...

  6. 自定义View(9)使用Renderscript 渲染特效。

    1.渲染脚本官网 https://developer.android.com/guide/topics/renderscript/compute 2.高斯模糊 ScriptIntrinsicBlur ...

  7. PHP开发之旅-提取表单提交内容发送邮件

    在实际项目开发中,我们经常需要得到用户的反馈信息并及时回复.普通的留言板有一定的内容限制,而邮件则能满足这个需求.今天给大家演示一下怎么利用PHP发送电子邮件. 1.创建表单 <form nam ...

  8. Android互动设计-蓝牙遥控自走车iTank

    一.让Android与外部的设备互动 iTank智能型移动平台基本款简介 iTank智能型移动平台是一台履带车,车体上方的控制板有一颗微处理器,我们可以通过它的UART或是I2C接口下达指令来控制iT ...

  9. 【PostgreSQL-9.6.3】临时表

    PostgreSQL中的临时表分两种,一种是会话级临时表,一种是事务级临时表.在会话级临时表中,数据可以存在于整个会话的生命周期中,在事务级临时表中的数据只能存在于事务的生命周期中.1. 会话级临时表 ...

  10. JS——scroll

    scrollWidth:父div宽度小于子div宽度,父div scrollWidth宽度为子div的宽度,大于则为本身的宽度width+padding scrollHeight:父div高度小于子d ...