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