预处理出每一行去掉$k$个1能获得的最小代价

之后做一次分组背包$dp$即可

预处理可以选择暴力枚举区间...

复杂度$O(n^3)$

#include <set>
#include <map>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define re register
#define ri register int
#define ll long long
#define tpr template <typename ra>
#define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
#define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)
#define gc getchar
inline int read() {
int p = , w = ; char c = gc();
while(c > '' || c < '') { if(c == '-') w = -; c = gc(); }
while(c >= '' && c <= '') p = p * + c - '', c = gc();
return p * w;
}
int wr[], rw;
#define pc(iw) putchar(iw)
tpr inline void write(ra o, char c = '\n') {
if(!o) pc('');
if(o < ) o = -o, pc('-');
while(o) wr[++ rw] = o % , o /= ;
while(rw) pc(wr[rw --] + '');
pc(c);
}
tpr inline void cmin(ra &a, ra b) { if(a > b) a = b; }
tpr inline void cmax(ra &a, ra b) { if(a < b) a = b; }
tpr inline bool ckmin(ra &a, ra b) { return (a > b) ? a = b, : ; }
tpr inline bool ckmax(ra &a, ra b) { return (a < b) ? a = b, : ; }
}
using namespace std;
using namespace remoon; #define sid 505 int n, m, k;
char s[sid][sid];
int pre[sid], suf[sid];
int f[sid][sid], g[sid][sid]; inline void Pre() {
rep(i, , n) {
memset(pre, , sizeof(pre));
memset(suf, , sizeof(suf));
rep(l, , m) pre[l] = pre[l - ] + (s[i][l] == '');
drep(l, m, ) suf[l] = suf[l + ] + (s[i][l] == '');
if(pre[m] == ) continue; memset(f[i], , sizeof(f[i]));
rep(l, , m) rep(r, l, m)
if(s[i][l] == '' && s[i][r] == '')
cmin(f[i][pre[l - ] + suf[r + ]], r - l + );
rep(j, pre[m], k) f[i][j] = ;
}
} inline void DP() {
memset(g, , sizeof(g));
g[][] = ;
rep(i, , n) {
rep(i1, , k) rep(i2, , k)
if(i1 + i2 <= k) cmin(g[i][i1 + i2], g[i - ][i1] + f[i][i2]);
}
int ans = 2e9;
rep(i, , k) cmin(ans, g[n][i]);
write(ans);
} int main() {
n = read(); m = read(); k = read();
rep(i, , n) scanf("%s", s[i] + );
Pre(); DP();
return ;
}

CF946D Timetable 动态规划的更多相关文章

  1. Codeforces 37D Lesson Timetable - 组合数学 - 动态规划

    题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...

  2. Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂

    花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable   tim ...

  3. [CF37D]Lesson Timetable_动态规划

    Lesson Timetable 题目链接:https://www.codeforces.com/contest/37/problem/D 数据范围:略. 题解: 根本就没想到可以动态规划. 首先从前 ...

  4. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  5. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  6. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  7. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  8. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  9. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

随机推荐

  1. [转]ROS(Robot Operating System)常用环境变量介绍

    本文简单介绍ROS系统中常用的环境变量用途及设置方式.ROS系统环境中除了必须配置的环境变量以外,其他的也是十分有用,通过修改变量路径,可以设置ROS系统中log文件存放路径,单元测试结果存放路径等. ...

  2. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  3. Spring---七大核心模块

    核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanFactory来产生和管理B ...

  4. Sicily 1211. 商人的宣传

    题目链接:http://soj.me/1211 Description Bruce是K国的商人,他在A州成立了自己的公司,这次他的公司生产出了一批性能很好的产品,准备宣传活动开始后的第L天到达B州进行 ...

  5. java数字转字符串前面自动补0或者其他数字

    /**  * Java里数字转字符串前面自动补0的实现.  *  * @author  xiaomo *  */  public class TestStringFormat {    public ...

  6. thinkphp报错Call to undefined method app\index\controller\Index::fetch()

    因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...

  7. 从Python到Web开发

    基础部分: 1-编程基础及Python环境部署 2-Python基础语法-内存管理-运算符-程序控制 3-Python内置结构-列表 4-Python数据类型之元组-字符串 5-python的封装与结 ...

  8. 2013 NEERC

    2013 NEERC Problem A. ASCII Puzzle 题目描述:完成一个拼图. solution 暴搜,但好像挺难打的,但听说因为题目限制比较多,其实很多奇怪的情况都不存在. Prob ...

  9. 使用UDP和TCP协议的各种应用和应用层协议

    IGMP和ICMP是传输层协议

  10. web性能优化之js图片懒加载

    html <div class="container"> <ul> <li> <div id="first" clas ...