Codeforces 946D Timetable DP

D. Timetable
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nmk 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?

Input

The first line contains three integers nm and k (1 ≤ n, m ≤ 5000 ≤ 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).

Output

Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.

Examples
input
Copy
2 5 1
01001
10110
output
5
input
Copy
2 5 0
01001
10110
output
8
Note

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.



某个快乐星球上一周有n天,一天有m小时,且对于每个整的小时,小明要么有课,要么没课。

小明一天从第一节课一直到最后一节课一直在学校停留。如果没课,就在家睡懒觉睡一天。问他如果翘掉k个课,在学校停留的最短时间。


我们发现直接求最短时间不太好求,可以转化为删掉K节课,求节省最多的时间。

对于某一天,贪心的删课只有两种选择:从最早的课开始向后删一些课,或者从最晚的课开始向前删。因为一节一节删课的话必定从两边删才能节省时间,如果不这样删,显然没有带来任何好处。

观察之后可以发现,对于每一天来说,删多少节课节省多少时间是互不影响的。由此很容易想到分天数DP,对于每一天单独考虑。用dp[i][j]表示考虑到第i天时,删除j门课最多节省的时间。由于之前讲的每一天之间是互不影响的,所以其实dp的第一维可以省去。

那么,用dp[i]表示删i节课节省的时间最多是多少,则对于任意一天,可以得到DP公式

dp[i]=max(dp[i],dp[i-j]+val[j])

其中j表示当前这天删掉了j节课,val[j]则是这一天删j节课节省的时间。我们只要求出val[j],就可以套用类似背包的方法dp求出最优解了。

每一天若删k课,则因为只能从两边删,所以只可能有k+1种情况,即0+k,1+(k-1),....k+0.

则把所有情况枚举求出val[k]即可。枚举一次复杂度O(n),求val[0..k]总的复杂度是O(n^2).

上面DP的复杂度同样是O(n^2)。一共有n天,dp n次,所以总的复杂度是O(n^3).


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=505,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int dp[maxn],val[maxn];
vector<int> v;

int main() {
	int n,m,k,sum=0,cnt=0,i,j,l;
	scanf("%d%d%d",&n,&m,&k);
	dp[0]=0;
	for (i=1;i<=n;i++) {
		v.clear();
		int first=-1,last=-1;
		for (j=1;j<=m;j++) {
			char c='a';
			while (!(c>='0'&&c<='9')) scanf("%c",&c);
			if (c=='1') {
				if (first==-1) first=j;
				last=j;
				v.push_back(j);
			}
		}
		if (first==-1) continue;
		sum+=last-first+1;
		for (j=1;j<=k;j++) val[j]=0;
		int size=v.size();
		for (j=1;j<size;j++) {
			for (l=0;l<=j;l++) {
				val[j]=max(val[j],v[l]-v[0]+v[size-1]-v[size-(j-l)-1]);
			}
		}
		val[size]=last-first+1;
		for (j=k;j>=1;j--) {
			for (l=1;l<=j;l++) {
				dp[j]=max(dp[j],dp[j-l]+val[l]);
			}
		}
	}
	printf("%d",sum-dp[k]);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值