#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

Posted LLbinGG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable相关的知识,希望对你有一定的参考价值。

2018-03-11

http://codeforces.com/contest/946/problem/D

D. Timetable
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

想法:虽然知道时背包,但是没啥思路,直接戳别人题解的解析:https://www.cnblogs.com/ZERO-/p/8530982.html

一些反思学习吧,有些不应该出现的错误还是会出现。增加刷题量和刷题频率,更重要的是要时时学习巩固算法了。

1.cf显示compication error 可能是头文件错误,比如这题忘写 cstring 而用了memset函数;

2.因为输入的是没有空格的连着的数字,先要将其当成字符;

3.数组越界问题;

4.特判一天的课全部都逃情况;

5.写背包时将计算最小值转化为计算最大值;

6.*怎么去预先处理每组的每种逃课情况下的最小天数(距离),不掌握的话,怕是知道什么算法也使不出来;

7.*学习分组背包;

code

 1 #include<cstring> 
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<vector> 
 6 #include<queue>
 7 using namespace std;
 8 #define maxn 505
 9 #define fi first
10 #define se second
11 #define ll long long
12  int h[505][505],p[505][505];
13     int num[505];
14     int a[505][505];
15     int dp[505];
16     int n,m,k;
17     char s[505][505]; 
18 int main()
19 {    
20    
21 
22     cin>>n>>m>>k;
23     memset(num,0,sizeof(num));
24     for(int i=0;i<n;i++)
25       cin>>s[i];
26     for(int i=0;i<n;i++)
27     { 
28       for(int j=0;j<m;j++)
29       {
30         h[i][j]=s[i][j]-\'0\';
31        if(h[i][j]==1)
32         {num[i]++;a[i][num[i]]=j;} //a[]存第几个1的位置 
33         
34       }
35     }  
36      for(int i=0;i<n;i++)  //第几排 
37      {
38          int tmp=min(k,num[i]); 
39          for(int j=0;j<=tmp;j++)  //下面要求对应逃课数的最短距离,j是选择的逃课数 j可能为0 
40          {
41             p[i][j]=maxn;          //p存的是最短距离 
42             if(j==num[i])          //易误点:当把全天要上的课逃完时,距离为0,要特判 
43             p[i][j]=0;
44            else     
45             for(int v=1;v<=j+1;v++)   //v是第几节要上的课 
46             {
47                  p[i][j]=min(p[i][j],a[i][v+num[i]-j-1]-a[i][v]+1); 
48                }     
49         }
50       }
51       ll sum=0;
52       for(int i=0;i<n;i++)
53         sum+=p[i][0];
54      memset(dp,0,sizeof(dp)); 
55       for(int i=0;i<n;i++)
56      //分组背包 
57        for(int j=k;j>=0;j--) 
58         for(int v=0;v<=min(k,num[i]);v++)        
59          if(j>=v)                                   //一开始没写,数组可能会越界,写上就对了。。。
60          dp[j]=max(dp[j-v]+p[i][0]-p[i][v],dp[j]);
61      cout<<sum-dp[k];    
62        
63 } 

 

 

以上是关于#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 109 (Rated for Div. 2)- D.Armchairs -01背包泛化物品

Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp

分组背包(通天之分组背包)

01背包完全背包多重背包分组背包总结

分组背包及树上分组背包

洛谷 P1757 通天之分组背包 分组背包