Title Link: Http://codeforces.com/problemset/problem/946/D
The main topic: There are n strings, representing the N-day timetable, 1 indicates that the time to class, 0 means not to class, one day in school time for the first class to the last class time. In total, you can escape the K-class and ask for at least how much time you need at school.
Problem-solving ideas: Listen to the big guy said backpack, and then pre-treatment think for more than 20 minutes, the end of the game, GG ... First preprocessing out v[i][k] that is, I day escape K class can save the most time, as to how to ask, directly enumerate K, and then enumerate the positions of both ends 1. Then you can do a group backpack, this will not say, very simple.
Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intn=1e3+5;8 Const intinf=0x3f3f3f3f;9 Ten CharS[n][n]; One intNum[n],pos[n][n],dp[n],v[n][n];//Pos[i][j] corresponds to the position of the first string J ' 1 ', num[i] records the number of the I-string ' 1 ' A //V[i][k] That is, paragraph I string delete K 1 Save time - intMain () { - intN,m,lim; thescanf"%d%d%d",&n,&m,&Lim); - for(intI=0; i<n;i++){ -scanf"%s", S[i]); - for(intj=0; j<m;j++){ + if(s[i][j]=='1'){ -num[i]++; +pos[i][num[i]]=J; A } at } - } - //process out v[i][k], - for(intI=0; i<n;i++){ - intCnt=0; - //Enumeration K in for(intk=0; K<=min (num[i]-1, Lim); k++){ - intres=INF; to //left into P 1, right into Q 1 + for(intp=0;p <=k;p++){ - intQ= (K-p); theRes=min (res,pos[i][num[i]-q]-pos[i][p+1]+1); * } $v[i][k]=m-Res;Panax Notoginseng } -v[i][num[i]]=m; the } + //Group Backpack AMemset (DP,0,sizeof(DP)); the for(intI=0; i<n;i++){ + for(intj=lim;j>=0; j--){ - for(intk=0; K<=min (j,m); k++){ $Dp[j]=max (dp[j],dp[j-k]+v[i][k]); $ } - } - } theprintf"%d\n", m*n-Dp[lim]); - return 0;Wuyi}
Codeforces 946D Timetable (pretreatment + group backpack)