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节课,问最少能在学校待多少时间. 分析 将一天的内容视作一个背包的组,可以预处理出该天内 ...
随机推荐
- YII中利用urlManager将URL改写成restful风格
这里主要涉及url显示样式 1.打开config文件夹下面的mian.php 2.修改内容 如把地址http://www.test.com/index.php?r=site/page/sid/ ...
- JavaScript--浅谈DOM操作
JavaScript之浅谈DOM操作 1.理解DOM: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两 ...
- dynamics 365 AI 解决方案 —— 介绍
Digital transformation has been reshaping our world and artificial intelligence (AI) is one of the n ...
- Vue 框架-05-动态绑定 css 样式
Vue 框架-05-动态绑定 css 样式 今天的小实例是关于 Vue 框架动态绑定 css 样式,这也是非常常用的一个部分 首先说一下 动态绑定,相对的大家都知道静态绑定,静态绑定的话,直接加 cl ...
- this and super
this 和 super 的区别:this, 先从本类找属性和方法,本类找不到再从父类找.super, 从父类找. this 和 super 都可以调用构造方法,所以this() 和 super() ...
- lsnrctl 与 tnsnames.ora 的联系
平台:Windoxs XP+Oracle 11G 当使用oralce的 Net Manager创建了一个名为“L3”的Listener后,要想使用lsnrctl启动和关闭 L3 还必须在tnsname ...
- Oracle SQL_TRACE使用小结
一.关于基础表Oc_COJ^c680758 rd-A6z/&[1R1] H680758 Oracle10G之前,启用AUTOTRACE功能需要手工创建plan_table表,创建脚本为$ORA ...
- springMVC入门-03
接着上一讲介绍springMVC针对rest风格的支持. 查询数据 使用前:/user_show?id=120 使用后:/user/120 删除数据 使用前:/user_delete?id=123 使 ...
- mysqlbinlog usage
[root@localhost mysql3306]# mysqlbinlogmysqlbinlog Ver 3.4 for el7 at x86_64Copyright (c) 2000, 2018 ...
- Asp.net Core相关教程及开源项目推荐
入门 全新的ASP.NET: https://www.cnblogs.com/Leo_wl/p/5654828.html 在IIS上部署你的ASP.NET Core项目: https://www.c ...