Timetable codeforces-946d (pretreatment + backpack)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.