CodeForces 946D Timetable (DP)
题意:给定 n,m,K,表示某个人一个周有 n 天,每天有 m 节课,但是他可以跳过 K 节课,然后下面每行一个长度为 m 个01字符串,0 表示该人在这一小时没有课,1 表示该人在这一个小时有课,每天的在学校时间是从开始上的第一节课,到上完最后一节课,问你他在校时间最短是多少。
析:首先要预处理出来他第 i 天跳过 j 节课在校的最短时间dp[i][j],因为每一天都是独立的,然后就可以使用动态规划来求解,f[i][j] 表示前 i 天跳过 j 节课的最短在校时间,f[i][j] = min{f[i-1][k] + dp[i][j-k]]},很容易就得到了,但是这个题我主要被卡在预处理上了,我开始使用贪心来解以为每次都从左边或者右边去掉 1,每次去掉间隔最大的,如果相等就继续向前比较,直到不相等为止,但这个思想是错的,有一个错误数据就是 K = 2 0110001101 这个串,对于K = 2 来说,应该是 4 ,但是用信心做却是 6,是不正确的,所以我们反向考虑,每次肯定是从左边或者右边去掉 1,最后剩下的肯定是连续的一部分,我们只要枚举所有剩下的一部分长度最短的就是答案。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define en end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int maxn = 500 + 50;
const int maxm = 1000 + 5;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int g[maxn][maxn];
int f[maxn][maxn];
int dp[maxn][maxn];
int a[maxn];
string s; int main(){
int K;
cin >> n >> m >> K;
ms(dp, INF);
for(int i = 1; i <= n; ++i){
cin >> s;
int cnt = 0;
for(int j = 0; j < m; ++j) if(s[j] == '1') a[cnt++] = j;
if(cnt) dp[i][0] = a[cnt-1] - a[0] + 1;
else dp[i][0] = 0;
dp[i][cnt] = 0;
int idx = cnt;
for(int j = 1; j < cnt; ++j) {
dp[i][j] = a[cnt-j-1] - a[0] + 1;
for(int k = 1; k + cnt - j - 1 < cnt; ++k){
dp[i][j] = min(dp[i][j], a[cnt+k-j-1] - a[k] + 1);
}
}
} for(int i = 1; i <= n; ++i){
for(int j = 0; j <= K; ++j){
f[i][j] = INF;
for(int k = 0; k <= j; ++k)
f[i][j] = min(f[i][j], f[i-1][k] + dp[i][j-k]);
}
}
cout << f[n][K] << endl;
return 0;
}
CodeForces 946D Timetable (DP)的更多相关文章
- Timetable CodeForces - 946D (区间dp)
大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间. 每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天 ...
- Codeforces 946D - Timetable (预处理+分组背包)
题目链接:Timetable 题意:Ivan是一个学生,在一个Berland周内要上n天课,每天最多会有m节,他能逃课的最大数量是k.求他在学校的时间最小是多少? 题解:先把每天逃课x节在学校呆的最小 ...
- Codeforces 946D Timetable(预处理+分组背包)
题目链接:http://codeforces.com/problemset/problem/946/D 题目大意:有n个字符串,代表n天的课表,1表示这个时间要上课,0表示不要上课,一天在学校时间为第 ...
- [Codeforces 946D]Timetable
Description 题库链接 给你一个 \(N\times M\) 的 \(01\) 矩阵,你可以从中将一些 \(1\) 变为 \(0\) ,最多 \(K\) 次.使操作之后使得每行最远的 \(1 ...
- CodeForces - 946D Timetable (分组背包+思维)
题意 n天的课程,每天有m个时间单位.若时间i和j都有课,那么要在学校待\(j-i+1\)个时间.现在最多能翘k节课,问最少能在学校待多少时间. 分析 将一天的内容视作一个背包的组,可以预处理出该天内 ...
- 2018.12.08 codeforces 946D. Timetable(背包)
传送门 题意简述:有一个人上n天课,每天有m个小时的时间安排表(一个01串),为1表示要上课,否则不上课,求出如果可以最多翘kkk节课这nnn天在校待的总时间的最小值(一天必须在所有课上完后才能离开) ...
- Timetable CodeForces - 946D (预处理+背包)
题意:n天m节课,最多可以逃k节课,每天在学校待的时间为该天上的第一节课到最后一节课持续的时间.问怎样逃课可以使这n天在学校待的时间最短,输出最短的时间. 分析: 1.预处理出每天逃j节课时在学校待的 ...
- codeforces 682D(DP)
题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...
- codeforces 666A (DP)
题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...
随机推荐
- xcode10 - 打ipa上蒲公英或者fire.im
1.选择空设备 2. 3. 4. 选择需要的 next 5. 6. 7. 8.选择位置 9. 选择ipa包 放到蒲公英 或者fire.im上 就行了
- Python 安装pyautogui
在Python中使用PyAutoGui模拟键盘和鼠标操作 一.系统环境 操作系统:win10 64位 Python版本:Python 3.7.0 二.安装参考 1.使用pip进行安装,pip inst ...
- saltstack 迭代项目到客户端并结合jenkins自动发布多台服务器
前面已经讲解了Webhook实现Push代码后的jenkins自动构建,接下来通过结合slatstack 实现多台机器的项目代码发布. 利用saltstack中file.recurse方法,运用该模块 ...
- docker-ce-17.09 仓库的创建与使用
docker仓库是集中存放镜像的地方,注册服务器是存放仓库的具体服务器,每个服务器上可以有多个仓库,每个仓库下面有多个镜像. 一.查找仓库中镜像 > docker search centos 二 ...
- 引入flash
调用代码 <div class="media"> <object class="main_video_box" classid="c ...
- Jmeter常用脚本开发之Beanshell Sampler
Beanshell Sampler Beanshell介绍:是一种完全符合java语法规范的脚本语言,且又拥有自己的一些语法和方法:是一种松散类型的脚本语言:它执行标准java语句和表达式,另外它还包 ...
- NSIS Error:Error writing temporary file. Make sure your temp folder is valid的解决办法
我的是Win7 32位,当前登录用户已经是管理员,在安装腾讯视频电脑版时,提示以上错误,无法安装. 查了好多资料,按如下方法解决了: 在win7下,以管理员的身份打开cmd(在window\syste ...
- mysql学习笔记-1.下载与安装
1.到mysql官网下载操作系统对应的数据库,下载地址https://dev.mysql.com/downloads/mysql/ 2.有msi安装版本和zip压缩版本,2种安装方式不同, 3.安装后 ...
- PDO 代码
<?php try{ $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root&qu ...
- hdu 1257 && hdu 1789(简单DP或贪心)
第一题;http://acm.hdu.edu.cn/showproblem.php?pid=1257 贪心与dp傻傻分不清楚,把每一个系统的最小值存起来比较 #include<cstdio> ...