Educational Codeforces Round 39: D. Timetable(DP)

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


题意:

一周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; 
}
/*
*/


根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值