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 ...
随机推荐
- 不通过调用__Init__来创建实例
老样子,抛出个问题,我们想要创建一个实例,但是由于某些原因想绕过__init__方法,用别的方式来进行创建. 举个栗子 小贱贱反序列化数据,或者说实现一个类方法将其作为备选的构造函数,都属于这种情况. ...
- echarts中间有字饼图Demo2
echarts链接:http://gallery.echartsjs.com/editor.html?c=xHy2vIPzLQ 完整代码: option = { backgroundColor: 'b ...
- TableViewCell去除选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableV ...
- linux查看目录下所有文件内容中是否包含某个字符串
转发自:http://blog.csdn.net/yimingsilence/article/details/76071949 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...
- sqljdbc4.jar的安装
自己项目环境(idea+jdk1.8+tomcat8),在搭建maven项目时,由于本地数据库是使用了sqlserver,所以需要项目与sqlserver之间建立连接,但是网上查的资料都说微软不允许以 ...
- golang语言中os包的学习与使用(文件,目录,进程的操作)
os中一些常用函数的使用: package main; import ( "os" "fmt" "time" "strings&q ...
- vue通过代理实现跨域
http://www.cnblogs.com/wangyongcun/p/7665687.html
- localstorage和vue结合使用
父组件 <template> <div class="hello"> <p>Original message:"{{message}} ...
- iOS.CM5.CM4.CM2
增量数据计算接口: CC_MDx_Init CC_MDx_Update CC_MDx_Final 全量数据计算接口: CC_MDx
- html 转化成 pdf