cf946d 怎样逃最多的课dp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf946d 怎样逃最多的课dp相关的知识,希望对你有一定的参考价值。

       来源:codeforces                                               D. Timetable

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?≤?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).

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




思路:
想暴力,不过只是想想而已
总结:
dp好难,和以前遇到的dp一样,都是dp[i][j]代表前i行,状态为j时的最优值,状态的表示比较难,nm数组用来表示当剩下i个1时要花的时间,
枚举所有逃课节数,再枚举当前行的逃课数量
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
char tl[501][501];
int dp[501][501],p[501],mn[501],n,m,kk,c,l;
int main()
{
        cin>>n>>m>>kk;
        for(int i=1;i<=n;i++)
        scanf("%s",tl[i]);
        for(int i=1;i<=n;i++)
        {
            c=0;
            for(int j=0;j<m;j++)
                if(tl[i][j]==1)
                    p[++c]=j;
            for(int j=1;j<=c;j++)
            {
                mn[j]=501;
                for(int k=1;k+j-1<=c;k++)
                    mn[j]=min(mn[j],p[k+j-1]-p[k]+1);
            }
          for(int j=0;j<=kk;j++)
          for(l=min(c,j),dp[i][j]=INF;l>=0;l--)
            dp[i][j]=min(dp[i][j],dp[i-1][j-l]+mn[c-l]);
        }
    cout<<dp[n][kk]<<endl;
    return 0;
}

 

以上是关于cf946d 怎样逃最多的课dp的主要内容,如果未能解决你的问题,请参考以下文章

Timetable CodeForces - 946D (预处理+背包)

Timetable CodeForces - 946D (区间dp)

cf837d Round Subset

cf1088E Ehab and a component choosing problem (树形dp)

CF D. Recovering BST (区间DP)

CF1000G Two-Paths