cf Educational Codeforces Round 39 D. Timetable

2024-03-24 07:58

本文主要是介绍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[i1][jt]+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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/840937

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

cf 164 C 费用流

给你n个任务,k个机器,n个任务的起始时间,持续时间,完成任务的获利 每个机器可以完成任何一项任务,但是同一时刻只能完成一项任务,一旦某台机器在完成某项任务时,直到任务结束,这台机器都不能去做其他任务 最后问你当获利最大时,应该安排那些机器工作,即输出方案 具体建图方法: 新建源汇S T‘ 对任务按照起始时间s按升序排序 拆点: u 向 u'连一条边 容量为 1 费用为 -c,

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

CF 508C

点击打开链接 import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String [] args){new Solve().run() ;} }class Solve{int bit[] = new int[608] ;int l

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

【CF】C. Glass Carving(二分 + 树状数组 + 优先队列 + 数组计数)

这题简直蛋疼死。。。。。 A了一下午 #include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 200005;int h,w,n;int C1[maxn],C2[maxn];int

【CF】E. Anya and Cubes(双向DFS)

根据题意的话每次递归分3种情况 一共最多25个数,时间复杂度为3^25,太大了 我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果 之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以 时间复杂度降低为 3 ^ (n/2+1) 题目链接:http://codeforces.com/contest/525/problem/E #include<set