Timetable(分组背包)

描述

传送门:Educational Codeforces Round 39

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.

Example

Input Output
1
2
3
2 5 1
01001
10110
1
5
1
2
3
2 5 0
01001
10110
1
8

思路

  • 分组背包……乱搞一下就好。
  • $dp[i][j]$表示前$i$天翘了$j$节课的最少在校时间。
  • 然后对于每一天,枚举翘课的节数,可以用滑动窗口来得到最小的在校时间。然后转移就好了。
  • 比赛的时候没有想到滑动窗口,XJB写了个贪心的选择,炸了……

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin \
ios_base::sync_with_stdio(0); \
cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int N = 505;
int dp[N][N];
char s[N];

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
clr(dp, 0x3f);
clr(dp[0], 0);
int n, m, t;
cin >> n >> m >> t;
for (int i = 1; i <= n; i++)
{
cin >> s;
int tt = 0;
for (int j = 0; j < m; j++) tt += s[j] == '1';
for (int j = 0; j < tt; j++)
{
int mm = INF;
queue<int> q;
for (int k = 0; k < m; k++)
{
if (s[k] == '1') q.push(k);
if (q.size() == tt - j) mm = min(mm, q.back() - q.front() + 1), q.pop();
}
for (int k = j; k <= t; k++)
dp[i][k] = min(dp[i][k], dp[i - 1][k - j] + mm);
}
for (int k = tt; k <= t; k++)
dp[i][k] = min(dp[i][k], dp[i - 1][k - tt]);
}
cout << dp[n][t] << endl;
return 0;
}
捐助作者
0%