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 ...
随机推荐
- 32位Server2008添加IIS
- connot connect to mysql 10061
根据我自己运行的情况,解决方法如下: 按windows+R, 输入services.msc查找服务,在服务与应用中找到MYsql服务,查看是否已启动.
- PL/SQL的快捷键设置
PL/SQL用来连接Oracle数据库的一种工具,它可以设置快捷方式,以便于我们快速的操作. PL/SQL设置快捷键 tools->Preferences(首选项)->User In ...
- RibbonControl 工具栏上的一些基本操作
1:左上角图标的属性项 应用程序ico标 ribboncontrol默认 左上角图标区域隐藏,先转换成 ribbonFrom 然后区域出现 下一步修改此区域ico:右键ribbonControl1 属 ...
- L1-006 连续因子(20)(思路+测试点分析)
L1-006 连续因子(20 分) 一个正整数 N 的因子中可能存在若干连续的数字.例如 630 可以分解为 3×5×6×7,其中 5.6.7 就是 3 个连续的数字.给定任一正整数 N,要求编写程序 ...
- hdu 1257 && hdu 1789(简单DP或贪心)
第一题;http://acm.hdu.edu.cn/showproblem.php?pid=1257 贪心与dp傻傻分不清楚,把每一个系统的最小值存起来比较 #include<cstdio> ...
- Oracle数据库mybatis 插入空值时报错(with JdbcType OTHER)
参考文档: 1.https://blog.csdn.net/fishernemo/article/details/27649233 2.http://helgaxu.iteye.com/blog/21 ...
- hihoCoder1159 扑克牌
一道记忆化搜索 原题链接 和着色方案很像,这里就不详细阐述,可以去我博客里的着色方案里看. 但要注意本题不一样的是同种面值的牌花色不同,所以在转移时还需要乘上同种面值的牌的个数. #include&l ...
- 卸载服务器GitLab
sudo gitlab-ctl uninstall sudo rpm -e gitlab-ce find / -name gitlab|xargs rm -rf
- Spring 循环引用(一)一个循环依赖引发的 BUG
Spring 循环引用(一)一个循环依赖引发的 BUG Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 循环 ...