2018-03-11

http://codeforces.com/contest/946/problem/D

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
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note

In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.

In the second example Ivan can't skip any lessons, so he spends 4 hours every day.

想法:虽然知道时背包,但是没啥思路,直接戳别人题解的解析:https://www.cnblogs.com/ZERO-/p/8530982.html

一些反思学习吧,有些不应该出现的错误还是会出现。增加刷题量和刷题频率,更重要的是要时时学习巩固算法了。

1.cf显示compication error 可能是头文件错误,比如这题忘写 cstring 而用了memset函数;

2.因为输入的是没有空格的连着的数字,先要将其当成字符;

3.数组越界问题;

4.特判一天的课全部都逃情况;

5.写背包时将计算最小值转化为计算最大值;

6.*怎么去预先处理每组的每种逃课情况下的最小天数(距离),不掌握的话,怕是知道什么算法也使不出来;

7.*学习分组背包;

code

 #include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define maxn 505
#define fi first
#define se second
#define ll long long
int h[][],p[][];
int num[];
int a[][];
int dp[];
int n,m,k;
char s[][];
int main()
{ cin>>n>>m>>k;
memset(num,,sizeof(num));
for(int i=;i<n;i++)
cin>>s[i];
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
h[i][j]=s[i][j]-'';
if(h[i][j]==)
{num[i]++;a[i][num[i]]=j;} //a[]存第几个1的位置 }
}
for(int i=;i<n;i++) //第几排
{
int tmp=min(k,num[i]);
for(int j=;j<=tmp;j++) //下面要求对应逃课数的最短距离,j是选择的逃课数 j可能为0
{
p[i][j]=maxn; //p存的是最短距离
if(j==num[i]) //易误点:当把全天要上的课逃完时,距离为0,要特判
p[i][j]=;
else
for(int v=;v<=j+;v++) //v是第几节要上的课
{
p[i][j]=min(p[i][j],a[i][v+num[i]-j-]-a[i][v]+);
}
}
}
ll sum=;
for(int i=;i<n;i++)
sum+=p[i][];
memset(dp,,sizeof(dp));
for(int i=;i<n;i++)
//分组背包
for(int j=k;j>=;j--)
for(int v=;v<=min(k,num[i]);v++)
if(j>=v) //一开始没写,数组可能会越界,写上就对了。。。
dp[j]=max(dp[j-v]+p[i][]-p[i][v],dp[j]);
cout<<sum-dp[k]; }

#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable的更多相关文章

  1. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  2. codeforces Educational Codeforces Round 39 (Rated for Div. 2) D

    D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number

    题: OvO http://codeforces.com/contest/946/problem/E CF 946E 解: 记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始 ...

  4. Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]

    https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  7. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  8. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

随机推荐

  1. python中的张量运算(tensor)

    1,首先比较二者的参数部分:这就是处理0阶张量和1阶张量的区别 np.max:(a, axis=None, out=None, keepdims=False) 求序列的最值 最少接收一个参数 axis ...

  2. Java课程寒假之开发记账本软件(网页版)之五

    一.实现基本功能之后 可以添加其他功能,比如说添加账户,删除账户,以及查询页面的分页.(我都没写,滑稽) 二.基本功能部分截图

  3. vue v-for的数组改变导致页面不渲染解决方法

    直接在数组里,改变数组来达到重新渲染页面的目的, 需要用push等数组方法, 或者$set(),或者给数组重新赋值,来改变数组引用地址 而是直接索引= <body> <div id= ...

  4. python 3 往Excel 中的写入内容但不覆盖原内容

    EXCEL 写入数据保持原样式 import xlwt import xlrd import xlutils import xlutils.copy class ExcelHandle(): def ...

  5. 360自带--JS开发工具箱

    360自带–JS开发工具箱 360自带–JS开发工具箱 360自带–JS开发工具箱

  6. python摸爬滚打之day21---- 模块

    1.MD5加密模块 MD5是一种不可逆的加密算法, 是安全而且可靠的.  在某些网站上能够搜到MD5解密工具, 其实并没有解密工具, 而是"撞库"的方式. 网站将一些MD5数据保存 ...

  7. POJ3461 Oulipo 字符串

    正解:kmp/哈希 解题报告: 传送门! 这题其实就kmp板子,,,用来复习下kmp的太久没打了QAQ 所以kmp做法就不港了放个代码就是了QAQ #include<algorithm> ...

  8. 20.0-uC/OS-III移植

    1.CPU移植要求: 1) 处理器有对应的能产生可重入代码的C编译器2) 处理器支持中断且能提供周期性的中断(通常介于10到1000Hz之间). 3) 可以关中断和开中断4) 处理器支持存储和载入堆栈 ...

  9. 17.4-uC/OS-III消息管理(任务消息队列使用)

    任务消息队列跟任务信号量一样,均隶属于某一个特定任务, 不需单独创建,任务在则在, 只有该任务才可以接收这个任务消息队列的消息,其他任务只能给这个任务消息队列发送消息, 却不能接收.任务消息队列与(普 ...

  10. vmvare安装vmtools菜单灰色

    光驱和各种驱动器改为自动检测 将vmvaretools.gz文件解压 tar xvf vm...gz 然后进程解压目录运行 sudo ./vmware-install.pl 然后重启 reboot 这 ...