#分组背包 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 10100110110

Output

5

Input

2 5 00100110110

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 } 

原文地址:https://www.cnblogs.com/LLbinGG/p/8544797.html

时间: 2024-10-01 19:51:58

#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable的相关文章

Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]

https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的描述中,商和余数是通过连续的减法来计算的,即从rk−2中不断减去rk−1直到小于rk−1.一个更高效的做法是使用整数除法和模除来计算商和余数: rk ≡ rk−2 mod rk−1 在欧几里得定义的减法版本,取余运算被减法替换 原文地址:https://www.cnblogs.com/Roni-i/

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html