zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    nodejs简单用法一
    初识ege图形库
    安装Microsoft Visual 2010 sp1回滚的错误,无Windows Installer目录
    MongoDB学习要点
    在java中,静态的内部类和非静态内部类的区别
    医院监护系统的问题定义及分析系统可行性及数据定义方法
    机票预订系统的问题定义及分析系统可行性
    银行储蓄系统问题:问题定义及分析系统可行性
    银行储蓄系统:问题定义及分析系统可行性
    软件开发的早期阶段为什么进行可行性研究?应该从哪些方面研究系统的可行性?
  • 原文地址:https://www.cnblogs.com/carcar/p/8540688.html
Copyright © 2011-2022 走看看