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节课时在学校待的 ...
随机推荐
- 使用svn在github上下载文件夹
今天想在github上下载mybatis-generator的eclipse插件,可是如何在github上下载一个文件夹而不用把这个项目clone呢,搜了一下,发现可以直接用svn来下载 只需将将路径 ...
- AtCoder AGC022C Remainder Game (图论)
题目链接 https://atcoder.jp/contests/agc022/tasks/agc022_c 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...
- AcWing:110. 防晒(贪心)
有C头奶牛进行日光浴,第i头奶牛需要minSPF[i]到maxSPF[i]单位强度之间的阳光. 每头奶牛在日光浴前必须涂防晒霜,防晒霜有L种,涂上第i种之后,身体接收到的阳光强度就会稳定为SPF[i] ...
- Apicloud_(模板)登陆注册功能模板
项目已托管到Github上 传送门 不需要使用任何图片资源,需要用到SHA1.js库文件, Apicloud_(接口验证)用户注册头部信息X-APICloud-AppKey生成 传送门 项目全代码放到 ...
- Android_(服务)Vibrator振动器
Vibrator振动器是Android给我们提供的用于机身震动的一个服务,例如当收到推送消息的时候我们可以设置震动提醒,也可以运用到游戏当中增强玩家互动性 运行截图: 程序结构 <?xml ve ...
- vue 组件 Vue.component 用法
todo https://blog.csdn.net/weixin_41796631/article/details/82929139
- koa2,koa-jwt中token验证实战详解
用户身份验证通常有两种方式,一种是基于cookie的认证方式,另一种是基于token的认证方式.当前常见的无疑是基于token的认证方式.以下所提到的koa均为koa2版本. token认证的优点是无 ...
- HTMLHint 配置文件
HTMLHint 工具可以对 HTML 代码做静态代码检查,从而保证 HTML 代码的规范和质量.HTMLHint 工具内置 23 条规则,建议在 .htmlhintrc 配置文件中将规则尽可能都打开 ...
- LeetCode19----删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- var $this = $(this)
jQuery: What’s the Difference Between $(this), $this, and this? What about $this? $this is a little ...