1. 程式人生 > >codeforces 946d D. Timetable(預處理+ 分組揹包)

codeforces 946d D. Timetable(預處理+ 分組揹包)

D. Timetabletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

ExamplesinputCopy
2 5 1
01001
10110
output
5
inputCopy
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 節  ,不然就GG 了。 這裡給出你每天的m 個小時是否有課。1表示有  0 沒有。(011000101   前邊的課去了的話,中間沒有課的時間也要待在學校裡  就比如  你翹了第一節課 那麼最短時間為7  翹了第一節第二節 最短時間為3 ) 你要算出小明最少的上課時間,

思路  : 我們可以將 最大翹課量看成一個揹包  ,但是每天 翹課的數目只能是一種 ,所以給你可以用分組揹包來解題。   那麼我們就要預處理出 一個數組 t[i][j] 表示第i天翹j 節課  那麼今天要去上學的最短時間。  找到 不翹課需要在學校的時間sum  sum-dp[k]就是 答案  dp[i]  表示  翹掉 i 節課 能翹掉的最大時間。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 505
#define inf 0x3f3f3f3f

using namespace std;

int dp[N];
int pos[N][N];
int cnt[N];
int t[N][N];
char s[N][N];

int n,m,k;

void init()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='1')
			{
				cnt[i]++;pos[i][cnt[i]]=j;
			}
		}
	}
	
	/*
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=cnt[i];j++) printf("%d ",pos[i][j]);
		printf("\n");
	}
	*/
	
	memset(t,inf,sizeof(t));
	
	for(int i=1;i<=n;i++)
	{
		int minn=min(k,cnt[i]);
		for(int j=0;j<=minn;j++)//biaoshi qiao ji jie ke 
		{
			for(int m=0;m<=j;m++)
			{
				if(j==cnt[i]) t[i][j]=0;
				else t[i][j]=min(t[i][j],pos[i][cnt[i]-m]-pos[i][1+j-m]+1);
			}
		}
	}
	
	return ;
}

int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]);
	}
	
	init();
	
	int sum=0;
	for(int i=1;i<=n;i++) sum+=t[i][0];
	
	/*
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=min(k,cnt[i]);j++) printf("%d ",t[i][j]);
		printf("\n");
	}
	*/
	
	for(int i=1;i<=n;i++)
	{
		for(int j=k;j>=0;j--)
		{
			int minn=min(k,cnt[i]);
			for(int m=0;m<=minn;m++)
			{
				if(j>=m) dp[j]=max(dp[j],dp[j-m]+(t[i][0]-t[i][m]));
			}
		}
	}
	
	printf("%d\n",sum-dp[k]);
	return 0;
}