D. Timetable time limit per test 2 seconds memory limit per test megabytes input standard input Output standard OU Tput
Ivan is a student at Berland state University (BSU). There is n days in Berland week, and each of the these days Ivan might has some classes at the university.
There is 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 was during j-th hour, then he spends j-i + 1 hour s in the University during. If There is 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 had 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 would enter the university right BEF Ore 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.
Given N, M, K and Ivan ' s timetable, can you determine the minimum number of hours he had to spend in the university during One week, if he cannot skip more than K lessons? Input
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 was 1, then Ivan had a lesson on i-th day during j-th hour (if it was 0, there is no such les son). Output
Print the minimum number of hours Ivan have to spend in the university during the week if he skips isn't more than K lessons. Examples input Copy
2 5 1
01001
10110
Output
5
Input Copy
2 5 0
01001
10110
Output
8
Test instructions
N Days a week, M hours per day (don't ask why it can not be 7 and 24. ), the position (I, J) is 1 for the first day of the J-hour class
For each day, you are going to the first class to go to school, the last class at the end of the moment to go home, of course, you can escape a week a total of K-Lessons
Find out how many hours to stay at school
N, M, K are only 500, you have to find out save[x][y] for the first X days to escape the Y class can stay at home for as long as possible
This can be a direct violence all in the school area, and then count the number of 1, but don't forget the interval can be empty (1 lessons in the same day)
Then Dp[a][b] said that before a day has escaped the B class, at home can stay at most, there
DP[A][B] = max (Dp[a][b], dp[a-1][q]+save[a][b-q], Q∈[0, j]);
The answer is 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;
}
/*
*/