Educational Codeforces Round 39: D. Timetable(DP)

來源:互聯網
上載者:User
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 Copy

2 5 10100110110
output
5
input Copy
2 5 00100110110
output
8


題意:

一周n天,每天m小時(別問為什麼可以不是7和24。。),位置(i, j)為1表示第i天第j小時有課

對於每一天,你都是要上第一節課時去學校,最後一節課上完瞬間回家,當然你一周可以逃掉總共k節課

求出在學校至少要呆多少小時


n, m, k都只有500,你要先求出save[x][y]表示第x天逃掉y節課最多可以在家呆多久

這個可以直接暴力所有在學校的區間,然後數裡面1的個數,不過別忘了區間可以為空白(當天1節課也不上)

然後dp[a][b]表示前a天已經逃掉了b節課,在家能最多呆多久,有

dp[a][b] = max(dp[a][b], dp[a-1][q]+save[a][b-q],  q∈[0, j]);

答案就是n*m-max(dp[n][i], i∈[0, k])


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[505][505], sum[505][505], bet[505][505], dp[505][505];int main(void){int i, j, k, n, m, V, temp, ans;scanf("%d%d%d", &n, &m, &V);for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%1d", &a[i][j]);sum[i][j] = sum[i][j-1]+a[i][j];}}for(i=1;i<=n;i++){for(j=1;j<=m;j++){for(k=j-1;k<=m;k++){temp = sum[i][m]-sum[i][k]+sum[i][j-1];bet[i][temp] = max(bet[i][temp], m-(k-j+1));}}}for(i=1;i<=n;i++){for(j=0;j<=V;j++){for(k=0;k<=j;k++)dp[i][j] = max(dp[i][j], dp[i-1][k]+bet[i][j-k]);}}ans = 0;for(i=0;i<=V;i++)ans = max(ans, dp[n][i]);printf("%d\n", n*m-ans);return 0; }/**/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.