cf946d 怎样逃最多的课dp
来源:codeforces D. Timetable
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
2 5 1
01001
10110
5
2 5 0
01001
10110
8 思路:
想暴力,不过只是想想而已
总结:
dp好难,和以前遇到的dp一样,都是dp[i][j]代表前i行,状态为j时的最优值,状态的表示比较难,nm数组用来表示当剩下i个1时要花的时间,
枚举所有逃课节数,再枚举当前行的逃课数量
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
char tl[][];
int dp[][],p[],mn[],n,m,kk,c,l;
int main()
{
cin>>n>>m>>kk;
for(int i=;i<=n;i++)
scanf("%s",tl[i]);
for(int i=;i<=n;i++)
{
c=;
for(int j=;j<m;j++)
if(tl[i][j]=='')
p[++c]=j;
for(int j=;j<=c;j++)
{
mn[j]=;
for(int k=;k+j-<=c;k++)
mn[j]=min(mn[j],p[k+j-]-p[k]+);
}
for(int j=;j<=kk;j++)
for(l=min(c,j),dp[i][j]=INF;l>=;l--)
dp[i][j]=min(dp[i][j],dp[i-][j-l]+mn[c-l]);
}
cout<<dp[n][kk]<<endl;
return ;
}
cf946d 怎样逃最多的课dp的更多相关文章
- BZOJ.2660.[BJOI2012]最多的方案(DP)
题目链接 首先我们知道: 也很好理解.如果相邻两项出现在斐波那契表示法中,那它们显然可以合并. 所以我们能得到\(n\)的斐波那契表示,记\(pos[i]\)为\(n\)的斐波那契表示法中,第\(i\ ...
- 大逃杀(树上dp)
这道题和宝藏差不多吧,转移的时候比较麻烦的. 代码中分量很多种情况. h更新比较麻烦 这两幅图表示了双边更新中3,4连个h更新,下面比较好理解的吧. #include<cstring> # ...
- 动态规划之经典数学期望和概率DP
起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...
- dp杂题(根据个人进度选更)
----19.7.30 今天又开了一个新专题,dp杂题,我依旧按照之前一样,这一个专题更在一起,根据个人进度选更题目; dp就是动态规划,本人认为,动态规划的核心就是dp状态的设立以及dp转移方程的推 ...
- Timetable CodeForces - 946D (区间dp)
大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间. 每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天 ...
- poj-3616 Milking Time (区间dp)
http://poj.org/problem?id=3616 bessie是一头工作很努力的奶牛,她很关心自己的产奶量,所以在她安排接下来的n个小时以尽可能提高自己的产奶量. 现在有m个产奶时间,每个 ...
- Hackers' Crackdown( UVA UVA 11825状压dp)
题意:N台电脑,现在有N种服务,现在你可以在每台电脑终止一项服务,他和他相邻的电脑都会被关闭,如果一项服务在所有电脑都没运行,该项服务成功被破坏,问最多能破坏几种服务. 分析:把n个集合分成尽量多组, ...
- POJ 2029 DP || 暴力
在大矩形中找一个小矩形 使小矩形包括的*最多 暴力或者DP 水题 暴力: #include "stdio.h" #include "string.h" int ...
- dp之背包总结篇
//新手DP学习中 = =!! 前言:背包问题在dp中可以说是经典,作为一个acmer,到现在才正式学习dp,可以说是比较失败的.我个人比较认同一点,想要做一个比较成功的acmer,dp.搜索.数学必 ...
随机推荐
- cpio的用法
cpio 这个命令挺有趣的,因为 cpio 可以备份任何东西,包括装置设备文件.不过 cpio 有个大问题, 那就是 cpio 不会主动的去找文件来备份!啊!那怎办?所以罗,一般来说, cpio 得要 ...
- 【Linux】磁盘的分区与命名规则
1,Startx:切换到图形界面 2,Pwd:查看当前目录 3,Whoami:查看当前用户 4,Cd: 1. 命令格式: cd [目录名] 2. 命令功能: 切换当前目录至dirNam ...
- MySQL 1130 - Host 127.0.0.1 is not allowed to connect to this MySQL server
在开发中为了让开发更方便,在本地配置环境,希望可以直接访问服务器上的MySQL数据库,更方便的管理数据库, 需要在本地远程连接linux服务器的本地数据库,直接用数据库管理工具连接出现如下报错1130 ...
- C++基础算法学习——N皇后问题
n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在n*n的棋盘上,互相不能攻击,输出全部方案. 代码如下: #include <iostream> #include<cmath& ...
- 【Beta Scrum】冲刺! 3/5
1. 今日完成情况 人员 学号 分工 是否完成 完成情况 胡武成 031502610 完成登录注册api,协助web端完成api N SDN课程实践加上课缘故,仅仅写完了登录注册的逻辑判断内容,但是短 ...
- web自动化-窗口句柄及位置变化
在进行web自动化时,很容易会遇到多窗口进行切换测试,下面就对多窗口的一些句柄和切换及窗口句柄顺序简单总结一下 from selenium import webdriver driver = webd ...
- IO_ObjectOutputStream(对象的序列化)
对象序列化就是将一些对象写入到硬盘中存储起来,以便下次复用 import java.io.FileInputStream; import java.io.FileOutputStream; impor ...
- WPFのImage控件souce引入的方法总结
1.后台代码相对路径添加(若为绝对路径,换UriKind的属性即可) BitmapImage testBitmapImage = new BitmapImage(new Uri(@"\bin ...
- Android——Intent和Intent过滤器
http://www.cnblogs.com/XP-Lee/p/3613830.html Intent就是一个激活组件的消息对象,用于组件之间的通信.需要注意的是,能被Intent激活通信的组件只有三 ...
- Scala 上下文界定
上下文界定的类型参数形式为T:M的形式,其中M是一个泛型,这种形式要求存在一个M[T]类型的隐式值: /** * 上下文界定 */ @Test def testOrdering_Class_Conte ...