本文主要是介绍cf Educational Codeforces Round 39 D. Timetable,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
D. Timetable
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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?
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 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
2 5 1
01001
10110
output
5
input
2 5 0
01001
10110
output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can’t skip any lessons, so he spends 4 hours every day.
中文:
有一个学生在大学上课,需要上n天课,一天的课程表是一个长度为m的二进制串,有课是1,没课是0,这个学生可以逃k节课。
现在问你,这个学生在最多逃了k节课的前提下,最少可以待在学校的时间是多少?
例如010110,如果他不逃课,可以在第二节开始上课,一直上到第5节,总共在学校呆4个小时。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=501;string TimeTable[maxn];
int Course[maxn][maxn];
int CourseNum[maxn];
int dp[maxn][maxn];
int Next[maxn];
int n,m,k;int SkipClass(string s,int skip,int tot)//逃了skip节课的最小在校时间,也就是上了tot-skip节课的最小在校时间
{if(skip>=tot)//逃课数大于总课数,不用来学校return 0;int InCourse=tot-skip;//上课数int f=m,e=0;if(skip==0)//不逃课{for(int i=0;i<m;i++){if(s[i]=='1'){f=i;break;}}for(int i=m-1;i>=0;i--){if(s[i]=='1'){e=i;break;}}if(e-f+1>=0)return e-f+1;//在学校多长时间elsereturn 0;}memset(Next,0,sizeof(Next));int pre,ans=INT_MAX;int ind=0,cnt=0;for(int i=0;i<m;i++){if(s[i]=='1')Next[ind++]=i;}pre=Next[0];ind=0;for(int i=0;i<m;i++)//用尺取法计算上tot-skip节课的最小区段,Next数组记录每一个需要上课位置的下标{if(s[i]=='1')cnt++;if(cnt==tot-skip){ans=min(ans,i-pre+1);pre=Next[++ind];cnt--;}}return ans;}
int main ()
{ios::sync_with_stdio(false);while(cin>>n>>m>>k){int cnt;for(int i=1;i<=n;i++){cin>>TimeTable[i];cnt=0;for(int j=0;j<m;j++){if(TimeTable[i][j]=='1')cnt++;}CourseNum[i]=cnt;}for(int i=1;i<=n;i++){for(int j=0;j<=k;j++){Course[i][j]=SkipClass(TimeTable[i],j,CourseNum[i]);}}memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++){for(int j=0;j<=k;j++){dp[i][j]=0x3F3F3F3F;for(int t=0;t<=j;t++){dp[i][j]=min(dp[i][j],dp[i-1][j-t]+Course[i][t]);//第i天逃t节课的最小在校时间}}}cout<<dp[n][k]<<endl;}return 0;
}
思路:
简单的动态规划问题,很好想。
设置 dp[i][j] d p [ i ] [ j ] 表示前i天逃课j节课的最小在校时间是多少。
那么 dp[i][j]=min(dp[i][j],dp[i−1][j−t]+Course[i][t]);t<=j d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i − 1 ] [ j − t ] + C o u r s e [ i ] [ t ] ) ; t <= j
Course[i][t] C o u r s e [ i ] [ t ] 里面记录的是第i天逃了t节课最少可以在学校呆多长时间
这篇关于cf Educational Codeforces Round 39 D. Timetable的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!