Test instructions: N Days m class, the maximum can escape K class, every day in the school to stay in the sky for the first class to the last lesson lasted time. Ask how to skip class can make this n days in school to stay the shortest time, output the shortest time.
Analysis:
1, pre-treatment of the daily Escape J class in the shortest time of school stay. T[I][J]
2, Dp[i][j] for the first day to escape J class stay in school the shortest time.
#include <bits/stdc++.h>using namespace Std;const int maxn = + 10;const int INF = 0x3f3f3f3f;char PIC[MAXN][MAXN] ;vector<int> v[maxn];int t[maxn][maxn];int dp[maxn][maxn];int main () {int n, m, K; scanf ("%d%d%d", &n, &m, &k); for (int i = 1; I <= n; ++i) {scanf ("%s", Pic[i]); } for (int i = 1, i <= N; ++i) {for (int j = 0; j < m; ++j) {if (pic[i][j] = = ' 1 ') { V[i].push_back (j); }}} for (int i = 1; I <= n; ++i) {int len = v[i].size (); int mi = min (len, k); for (int j = 0; J <= mi; ++j) {//truancy int rest = Len-j; if (rest = = 0) {//No classes today t[i][j] = 0; Continue } int Mi = INF; for (int w = 0; w < Len; ++w) {int et = w + rest-1; if (et >= len) break; mi = min (mi, v[i][et]-v[i][w] + 1); } T[i][j] = Mi; }} for (int i = 1, i <= N; ++i) {for (int j = 0; j <= K; ++j) {dp[i][j] = INF; for (int w = 0; w <= J; ++w) {Dp[i][j] = min (Dp[i][j], dp[i-1][w] + t[i][j-w]); }}} printf ("%d\n", Dp[n][k]); return 0;}
Timetable codeforces-946d (pretreatment + backpack)