Timetable
Test instructions: Ivan is a student, their local week has n days, every day has M class, each class an hour, then ' 1 ' represents Ivan this time period needs class, now Ivan can skip the choice K class not to go on, he in school time for the day he chose the first lesson to the last class, That is, you can jump 2 end of the class, if the day without classes, the day will not need to go to school. Now, what is the minimum time for Ivan to go to school this week?
Solution: Calculate the total time not to skip class, and then use Group backpack (because there is only one case per day, so can only choose one in each group), run out of Class K section of the most truancy time is how much, the total time-truancy time is the answer.
Code:
1#include <bits/stdc++.h>2 using namespacestd;3 #defineLL Long Long4 #defineULL unsigned LL5 #defineFi first6 #defineSe Second7 #defineLson l,m,rt<<18 #defineRson m+1,r,rt<<1|19 #defineMax3 (A,B,C) max (A,max (b,c))Ten Const intINF =0x3f3f3f3f; One ConstLL mod = 1e9+7; Atypedef pair<int,int>PLL; - Const intN = 5e3+Ten; - CharStr[n]; the intDp[n]; - intPos[n]; - intLen[n]; - intN, M, K; + intMain () { -scanf"%d%d%d", &n, &m, &k); + inttot =0; A for(inti =1; I <= N; i++) { atscanf"%s", str+1); - intCNT =0; - for(intj =1; J <= M; J + +) - if(Str[j] = ='1') -POS[++CNT] =J; - if(CNT = =0)Continue; inmemset (Len,0,sizeof(len)); - intLlen = pos[cnt]-pos[1]+1; toTot + =Llen; + for(intto =1; To <= K && to <= CNT; to++){ - for(intr = cnt-to, L =1; R >= L && R <= CNT; r++, l++) theLen[to] = max (Len[to], Llen-(Pos[r]-pos[l] +1)); * if(To = =CNT) $Len[to] =Llen;Panax Notoginseng } - for(intj = k; J >=1; j--) the for(intto =1; To <= J && to <= CNT; to++) +DP[J] = max (Dp[j], dp[j-to]+len[to]); A } theprintf"%d", Tot-dp[k]); + return 0; -}
Codeforces 946D Timetable