D. Timetable
http://codeforces.com/problemset/problem/946/D
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
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
// 去吧!皮卡丘! 把AC带回来!
// へ /|
// /\7 ∠_/
// / │ / /
// │ Z _,< / /`ヽ
// │ ヽ / 〉
// Y ` / /
// イ● 、 ● ⊂⊃〈 /
// () へ | \〈
// >ー 、_ ィ │ //
// / へ / ノ<| \\
// ヽ_ノ (_/ │//
// 7 |/
// >―r ̄ ̄`ー―_
//**************************************
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
#define pi acos(-1)
#define mem(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = 3e5 + ;
// name*******************************
int n, m;
int a[][];
int K;
int kk[][];//kk[i][j]记录第i行删掉j个的最小代价,为后面dp服务
int dp[][];//dp[i][j]:前i行,共删掉j个所获最下代价
int cnt[];//记录每组的1的个数
int sum=; //sum这里记录下1的总个数,若K大于sum,直接输出0
// function****************************** //***************************************
int main() {
// ios::sync_with_stdio(0); cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin >> n >> m >> K;
me(dp, );
me(kk, );
For(i, , n) {
For(j, , m) {
int x;
scanf("%1d", &x);//限宽输入
if (x) {
a[i][++cnt[i]] = j;
}
}
//选定j,k为起始终止点
For(j, , cnt[i]) {
For(k, j, cnt[i]) {
kk[i][(j - ) + cnt[i] - k] =
min(kk[i][(j - ) + cnt[i] - k], a[i][k] - a[i][j] + );
}
}
kk[i][cnt[i]] = ;
sum+=cnt[i];
}
if(K>=sum){
cout<<;
return ;
}
For(i, , min(K, cnt[])) {
dp[][i] = kk[][i];
}
For(i, , n) {
For(j, , K) {
For(k, , min(j,cnt[i])) {
dp[i][j] = min(dp[i][j], dp[i - ][j - k] + kk[i][k]);
}
}
}
cout << dp[n][K]; return ;
}
D. Timetable的更多相关文章
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2)  D. Timetable
		
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
 - Codeforces 946D - Timetable (预处理+分组背包)
		
题目链接:Timetable 题意:Ivan是一个学生,在一个Berland周内要上n天课,每天最多会有m节,他能逃课的最大数量是k.求他在学校的时间最小是多少? 题解:先把每天逃课x节在学校呆的最小 ...
 - Codeforces Round #581 (Div. 2)A	 BowWow and the Timetable (思维)
		
A. BowWow and the Timetable time limit per test1 second memory limit per test256 megabytes inputstan ...
 - Distributed Databases and Data Mining: Class timetable
		
Course textbooks Text 1: M. T. Oszu and P. Valduriez, Principles of Distributed Database Systems, 2n ...
 - [Codeforces 946D]Timetable
		
Description 题库链接 给你一个 \(N\times M\) 的 \(01\) 矩阵,你可以从中将一些 \(1\) 变为 \(0\) ,最多 \(K\) 次.使操作之后使得每行最远的 \(1 ...
 - Codeforces 37D Lesson Timetable - 组合数学 - 动态规划
		
题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...
 - 2018.12.08 codeforces 946D. Timetable(背包)
		
传送门 题意简述:有一个人上n天课,每天有m个小时的时间安排表(一个01串),为1表示要上课,否则不上课,求出如果可以最多翘kkk节课这nnn天在校待的总时间的最小值(一天必须在所有课上完后才能离开) ...
 - CodeForces 946D Timetable (DP)
		
题意:给定 n,m,K,表示某个人一个周有 n 天,每天有 m 节课,但是他可以跳过 K 节课,然后下面每行一个长度为 m 个01字符串,0 表示该人在这一小时没有课,1 表示该人在这一个小时有课,每 ...
 - CodeForces - 946D Timetable (分组背包+思维)
		
题意 n天的课程,每天有m个时间单位.若时间i和j都有课,那么要在学校待\(j-i+1\)个时间.现在最多能翘k节课,问最少能在学校待多少时间. 分析 将一天的内容视作一个背包的组,可以预处理出该天内 ...
 
随机推荐
- 简单的PHP的任务队列
			
文章太长,不作过多介绍,反正,文章的头部就说明了大概的意思...原文如下:写了一个简单的队列任务处理.多进程任务,异步任务可能会用到这个(主要是命令行应用)比如,任务的某个一个环节速度十分不稳定,可能 ...
 - ComboBox Style
			
<SolidColorBrush x:Key="ComboBoxNormalBorderBrush" Color="#e3e9ef" /> < ...
 - Object equals 方法
			
package com.mydemo.controller; public class TestEquals { public static void main(String[] args) { Do ...
 - 类和类的关系——java
			
类(对象)之间的关系 1.继承关系(子类自动拥有了父类所有的成员变量和普通方法,如果父类的成员变量和普通方法是private 的,那么子类只有拥有权,没有使用权.父类的构造方法在子类的构造方法执行 ...
 - java变量常量
			
1. java 变量遵循先声明,再赋值,后使用的原则. 一个变量可以只声明,不赋值,没有问题(只是这个变量没有实际意义,但完全没有问题).但如果想要使用它,那么就一定要给它赋值,而大多数时候我们又不知 ...
 - Django Redis验证码 密码 session 实例
			
1.settings CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCach ...
 - Eigen学习
			
Eigen 是一个基于C++的线性代数库,其中实现大量常用的线性代数算法,包括常规矩阵计算,矩阵变换,矩阵分解,矩阵块操作.Eigen 广泛地应用在开源项目中,例如OpenCV,PCL(Point C ...
 - Oracle EBS 报错 您不具有分配给您的清除MDS的权限
 - flask 的管理模块的功能add_template_global、send_from_directory
			
add_template_global方法 全局模板函数 add_template_global 装饰器直接将函数注册为模板全局函数. add_template_global 这个方式是自定义的全局函 ...
 - Python项目生成requirements.txt的多种方式
			
我相信任何软件程序都会有依赖的类库,尤其现在开源如此的火爆,因为一个项目可能会有无很多的依赖的包 这个时候难道我们都要一个一个的去找到安装吗?即使你找到了依赖的包 但是呢模块的版本又有很多难道你都要装 ...