1. 程式人生 > >Educational Codeforces Round 39: D. Timetable(DP)

Educational Codeforces Round 39: D. Timetable(DP)

time limit per test 2 secondsmemory limit per test 256 megabytesinput standard inputoutput 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.

ExamplesinputCopy
2 5 1
01001
10110
output
5
inputCopy
2 5 0
01001
10110
output
8

題意:

一週n天,每天m小時(別問為什麼可以不是7和24。。),位置(i, j)為1表示第i天第j小時有課

對於每一天,你都是要上第一節課時去學校,最後一節課上完瞬間回家,當然你一週可以逃掉總共k節課

求出在學校至少要呆多少小時

n, m, k都只有500,你要先求出save[x][y]表示第x天逃掉y節課最多可以在家呆多久

這個可以直接暴力所有在學校的區間,然後數裡面1的個數,不過別忘了區間可以為空(當天1節課也不上)

然後dp[a][b]表示前a天已經逃掉了b節課,在家能最多呆多久,有

dp[a][b] = max(dp[a][b], dp[a-1][q]+save[a][b-q],  q∈[0, j]);

答案就是n*m-max(dp[n][i], i∈[0, k])

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[505][505], sum[505][505], bet[505][505], dp[505][505];
int main(void)
{
	int i, j, k, n, m, V, temp, ans;
	scanf("%d%d%d", &n, &m, &V);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			scanf("%1d", &a[i][j]);
			sum[i][j] = sum[i][j-1]+a[i][j];
		}
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			for(k=j-1;k<=m;k++)
			{
				temp = sum[i][m]-sum[i][k]+sum[i][j-1];
				bet[i][temp] = max(bet[i][temp], m-(k-j+1));
			}
		}
	}
	for(i=1;i<=n;i++)
	{
		for(j=0;j<=V;j++)
		{
			for(k=0;k<=j;k++)
				dp[i][j] = max(dp[i][j], dp[i-1][k]+bet[i][j-k]);
		}
	}
	ans = 0;
	for(i=0;i<=V;i++)
		ans = max(ans, dp[n][i]);
	printf("%d\n", n*m-ans);
	return 0; 
}
/*
*/