Timetable CodeForces - 946D (区间dp)
大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间.
每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天dp即可.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 510;
#else
const int N = 111;
#endif int n, m, k, tot;
char s[N][N];
int f[N][N], g[N][N], dp[N][N];
int main() {
scanf("%d%d%d", &n, &m, &k);
REP(i,1,n) scanf("%s",s[i]+1);
REP(i,1,n) {
vector<int> v;
REP(j,1,m) if (s[i][j]=='1') v.pb(j);
int sz=v.size();
if (sz) {
memset(dp,0,sizeof dp);
v.insert(v.begin(),0);
PER(len,1,sz-1) {
REP(L,1,sz+1-len) {
int R = L+len-1;
if (L!=1) dp[L][R]=max(dp[L][R],dp[L-1][R]+v[L]-v[L-1]);
if (R!=sz) dp[L][R]=max(dp[L][R],dp[L][R+1]+v[R+1]-v[R]);
f[i][sz-len]=max(f[i][sz-len],dp[L][R]);
}
}
f[i][sz] = f[i][sz-1]+1;
tot += v[sz]-v[1]+1;
}
REP(j,0,k) REP(jj,0,min(j,sz)) {
g[i][j]=max(g[i][j],g[i-1][j-jj]+f[i][jj]);
}
}
printf("%d\n", tot-g[n][k]);
}
Timetable CodeForces - 946D (区间dp)的更多相关文章
- CodeForces 512B(区间dp)
D - Fox And Jumping Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- codeforces 1140D(区间dp/思维题)
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 1114D(区间DP)
题面 传送门 分析 法1(区间DP): 首先,我们可以把连续的相等区间缩成一个数,用unique来实现,不影响结果 {1,2,2,3,3,3,5,3,4}->{1,2,3,5,3,4} 先从一个 ...
- CodeForces - 1107E 区间DP
和紫书上的Blocks UVA - 10559几乎是同一道题,只不过是得分计算不同 不过看了半天紫书上的题才会的,当时理解不够深刻啊 不过这是一道很好区间DP题 细节看代码 #include<c ...
- CodeForces 149D 区间DP Coloring Brackets
染色有三个条件: 对于每个点来说要么不染色,要么染红色,要么染蓝色 对于每对配对的括号来说,有且只有一个一边的括号被染色 相邻的括号不能染成相同的颜色 首先可以根据给出的括号序列计算出括号的配对情况, ...
- Zuma CodeForces - 607B (区间DP)
大意: 给定字符串, 每次删除一个回文子串, 求最少多少次删完. #include <iostream> #include <cstdio> #define REP(i,a,n ...
- Recovering BST CodeForces - 1025D (区间dp, gcd)
大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...
- Codeforces 940 区间DP单调队列优化
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Timetable CodeForces - 946D (预处理+背包)
题意:n天m节课,最多可以逃k节课,每天在学校待的时间为该天上的第一节课到最后一节课持续的时间.问怎样逃课可以使这n天在学校待的时间最短,输出最短的时间. 分析: 1.预处理出每天逃j节课时在学校待的 ...
随机推荐
- Windows下如何安装Redis
Redis可以从下面的github上面下载,当前的下载版本为3.2.100版本 https://github.com/MicrosoftArchive/redis/releases 这边都是64位的链 ...
- C++入门经典-例4.1-声明、定义和使用函数
1:代码如下: // 4.1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
- 黑马lavarel教程---6、简单验证
黑马lavarel教程---6.简单验证 一.总结 一句话总结: 1.验证的最简单实例:$request的validate方法,验证通过可以继续进行,验证失败就重定向 2.中文提示可以用中文语言包 3 ...
- Oracle数据表之间的数据同步
保证两个数据表结构相同,如不相同只能同步相同字段; 只是思路,具体请根据需求修改. declare cursor csrn_mon is select * from table2; row_mon c ...
- Kettle使用教程之Job使用
1.Kettle的Job使用十分简单,这里也只是演示比较简单的操作,创建Job 2.点击转换,然后点击浏览,选择转换对象 3.执行按钮,运行该转换 4.如果需要长期的进行定时转换,可以在Job中的st ...
- MYSQL5.7二进制包的安装
mysql5.7 二进制包安装1. 下载包 wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_6 ...
- 四十七:数据库之alembic数据库迁移工具的基本使用
在一般情况下,如果修改了模型,如增加或者删除了字段,SQLAlchemy是不会更新的,这就需要使用alembic来实现 使用alembic步骤:一:定义好模型二:使用alembic创建一个仓库:ale ...
- docker扩容
方法一. https://www.cnblogs.com/atuotuo/p/7217331.html 只需要修改 dockerd 的启动参数啊 增加 --data-root 把 /var/lib/d ...
- 【SVN】更新提交失败---- Previous operation has not finished; run 'cleanup' if it was interrupted解决方法
Previous operation has not finished; run 'cleanup' if it was interrupted 问题出处 解决方法 2017-11-01 08: ...