1. 程式人生 > >#分組背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

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

p s eterm queue erl 學習 () logs https 情況

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