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 nmk 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 nm 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 1
01001
10110
output
5
input

Copy
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.

题意 有n行 每行有m个0或1 删掉k个1 使上课时间最短

比赛的时候想的贪心,wa了一发之后发现贪心情况考虑不全。要用dp(分组背包)写,还是队友强,赛后马上补出来了

详细题解请看 http://www.cnblogs.com/ZERO-/p/8530982.html

错误代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 500+5;
const int inf = 0x3f3f3f3f;
const double epx = 1e-10;
typedef long long ll;
int n,m,k;
int a[maxn][maxn];
struct node
{
int l,r;
int li,ri;
}b[maxn];
void solve()
{
for(int i=1;i<=n;i++)
{
int fir,sec;
int flag;
fir=sec=flag=0;
for(int j=1;j<=m;j++)
{
if(a[i][j]==1)
{
if(flag==0)
fir=sec=j,flag=1;
else
{
sec=j;
break;
}
}
}
if(sec==0&&fir==0)
b[i].l=0,b[i].li=fir;
else if(sec==fir)
b[i].l=1,b[i].li=fir;
else
b[i].l=sec-fir,b[i].li=fir;
fir=sec=flag=0;
for(int j=m;j>=1;j--)
{
if(a[i][j]==1)
{
if(flag==0)
fir=sec=j,flag=1;
else
{
sec=j;
break;
}
}
}
if(sec==0&&fir==0)
b[i].r=0,b[i].ri=fir;
else if(sec==fir)
b[i].r=1,b[i].ri=fir;
else
b[i].r=fir-sec,b[i].ri=fir;
}
}
int main()
{
cin>>n>>m>>k;
int sum=0;
for(int i=1;i<=n;i++)
{
string temp;
cin>>temp;
int l=0,r=0,flag=0;
for(int j=0;j<temp.length();j++)
{
if(temp[j]=='1')
{
a[i][j+1]=1;
if(flag==0)
l=r=j+1,flag=1;
else
r=j+1;
}
else
a[i][j+1]=0;
}
if(l==0&&r==0)
continue;
else if(l==r)
sum+=1;
else
sum+=r-l+1;
}
solve();
// for(int i=1;i<=n;i++)
// cout<<b[i].l<<" "<<b[i].li<<" "<<b[i].r<<" "<<b[i].ri<<endl;
while(sum!=0&&k!=0)
{
int maxx=0;
int cnt=0,num=0;
for(int i=1;i<=n;i++)
{
if(b[i].l>maxx)
cnt=i,num=1,maxx=b[i].l;
if(b[i].r>maxx)
cnt=i,num=2,maxx=b[i].r;
}
sum-=maxx;
k--;
if(num==1)
a[cnt][b[cnt].li]=0;
else if(num==2)
a[cnt][b[cnt].ri]=0;
solve();
}
cout<<sum<<endl;
}

AC代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn=+;
const int inf=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int h[maxn][maxn],a[maxn][maxn];
int p[maxn][maxn],num[maxn],dp[maxn];
char s[maxn][maxn];
int main(){
int n,m,k;
ios;
cin>>n>>m>>k;
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]-'';
}
memset(num,,sizeof(num));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(h[i][j]==){
num[i]++;
a[i][num[i]]=j;
}
}
}
for(int i=;i<n;i++){
for(int j=;j<=min(k,num[i]);j++)
p[i][j]=inf;
}
for(int i=;i<n;i++){
int x=min(k,num[i]);
for(int j=;j<=x;j++){
for(int k=;k<=j;k++){
if(j==num[i])
p[i][j]=;
else
p[i][j]=min(p[i][j],a[i][num[i]-k]-a[i][+j-k]+);
}
}
}
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 h=;h<=min(k,num[i]);h++)
if(j>=h)
dp[j]=max(dp[j-h]+(p[i][]-p[i][h]),dp[j]);
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<=k;j++)
// {
// cout<<p[i][j]<<" ";
// }
// cout<<endl;
// }
// for(int i=0;i<=k;i++)
// {
// cout<<dp[i]<<endl;
// }
// cout<<sum<<endl;
cout<<sum-dp[k]<<endl;
}
//2 7 2
//0100101
//

codeforces Educational Codeforces Round 39 (Rated for Div. 2) D的更多相关文章

  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. #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

    2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...

  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. .NET框架概述

    .NET战略目标: 任何时候(when),任何地方(where),使用任何工具(what)都能通过.NET的服务获得网络上的任何信息. .NET优势: 1.提供了一个面向对象的编程环境,完全支持面向对 ...

  2. AJPFX关于abstract的总结

    抽象类: abstract抽象:不具体,看不明白.抽象类表象体现.在不断抽取过程中,将共性内容中的方法声明抽取,但是方法不一样,没有抽取,这时抽取到的方法,并不具体,需要被指定关键字abstract所 ...

  3. 前端:常见的6种HTML5错误用法

    一.不要使用section作为div的替代品 人们在标签使用中最常见到的错误之一就是随意将HTML5的<section>等价于<div>——具体地说,就是直接用作替代品(用于样 ...

  4. 如何安装sql server2005 windows 8

    如何安装sql server2005   windows 8 1 从网上下载到本地文件 ,这里使用的是cs_sql_2005_dev_all_dvd 安装版. 2. 点击下图所表示进行安装   3. ...

  5. Asp.Net 设计模式 之 单例模式

    一.设计目的:让项目中只显示一个实例对象 二.设计步骤: 创建一个类: 构建类类型静态变量: 定义返回值类为单例类型的静态方法: 判断静态变量instance是否为空:如果为空,就创建实例,然后给单例 ...

  6. ubuntu命令行使用ftp客户端

    转载 本篇文章主要介绍在Ubuntu 8.10下如何使用功能强大的FTP客户端软件NcFTP. Ubuntu的源里为我们提供了FTP客户端软件NcFTP,可这款工具对新手来说不是很方便.本文介绍的是一 ...

  7. h5编写帧动画

    var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame; var ...

  8. SQL SERVER系统表和常用函数介绍

    sysaltfiles 主数据库 保存数据库的文件 syscharsets 主数据库 字符集与排序顺序sysconfigures 主数据库 配置选项syscurconfigs 主数据库 当前配置选项s ...

  9. React Components之间的通信方式了解下

    先来几个术语: 官方 我的说法 对应代码 React element React元素 let element=<span>A爆了</span> Component 组件 cla ...

  10. 一键分享到QQ空间、QQ好友、新浪微博、微信代码

    通过qq空间.qq聊天.新浪微博和微信二维码分享平台提供的接口,实现把网页中对应的图片.标题.描述的信息参数用javascript获取后传进接口中,实现一键分享. 使用到的接口(测试时需要登录,网址和 ...