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 ...
随机推荐
- C++中的字符数组与字符指针
//[C++基础]字符数组和字符指针.cpp//剑指offer上的这段话://为了节省内存,c/c++把常量字符串放到单独的一个内存空间.但是当几个指针赋值给相同的常量字符串时,它们实际上会指向相同的 ...
- nginx配置websocket
有时候我们需要给websocket服务端做一下nginx的配置,比如需要给websocket服务端做负载均衡,或者,有些系统要求访问websocket的时候不能带端口,这时候我们就需要用nginx来进 ...
- day 25 udp, socketserver
建立UDP连接的示例: # server端 import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1', ...
- 编程:在屏幕中间分别显示绿色、绿底红色、白底蓝色的字符串'welcome to masm!'
80*25彩色字符模式显示缓冲区的结构: 内存地址空间中,B8000H~BFFFFH共32KB的空间,为80*25彩色字符模式的显示缓冲区.向这个地址空间写入数据,写入的内容将立即出现在显示器上. 在 ...
- visio2013专业版激活密匙
Visio 2013最新产品密钥分享,在安装时可以使用以下密钥: 2NYF6-QG2CY-9F8XC-GWMBW-29VV8 FJ2N7-W8TXC-JB8KB-DCQ7Q-7T7V3 VXX6C-D ...
- [Z]scp example
https://www.cnblogs.com/autumnvivi/articles/3447964.html
- 制作根文件系统之内核如何启动init进程
start_kernel其实也是内核的一个进程,它占用了进程号0,start_kernel的内容简写如下: asmlinkage void __init start_kernel(void) //内核 ...
- HTML标签归纳
首先,按下h1,再按下Tab键就可以变成<h1></h1>,Ctrl+d可以复制当前行,Ctrl+Alt+Insert可以快速新建文件,Home可以跳到当前行最前面,End同理 ...
- Android开发之动态设置字体的样式和粗细
字体设置通常有两种形式: 1:在xml中直接设置 android:textStyle="bold" android:typeface="sans" 2:用jav ...
- get(0).tagName获得作用标签
<script type="text/javascript" src="jquery1.4.js"></script><scrip ...