New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)
大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数
转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全无穷, 零元为全无穷
#include <iostream>
#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'
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;}
//head const int N = 2e5+10;
int n, q;
char s[N];
struct _ {
int v[5][5];
_ operator * (const _ & rhs) const {
_ r;
memset(r.v,0x3f,sizeof r.v);
REP(k,0,4) REP(i,0,4) REP(j,0,4) {
r.v[i][j] = min(r.v[i][j],v[i][k]+rhs.v[k][j]);
}
return r;
}
} mx[N<<2];
_ build(int o, int l, int r) {
if (l==r) {
memset(mx[o].v,0x3f,sizeof mx[o].v);
REP(i,0,4) mx[o].v[i][i]=0;
if (s[l]=='2') mx[o].v[0][0]=1,mx[o].v[0][1]=0;
if (s[l]=='0') mx[o].v[1][1]=1,mx[o].v[1][2]=0;
if (s[l]=='1') mx[o].v[2][2]=1,mx[o].v[2][3]=0;
if (s[l]=='7') mx[o].v[3][3]=1,mx[o].v[3][4]=0;
if (s[l]=='6') mx[o].v[3][3]=1,mx[o].v[4][4]=1;
return mx[o];
}
return mx[o]=build(ls)*build(rs);
}
_ qry(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return mx[o];
if (mid>=qr) return qry(ls,ql,qr);
if (mid<ql) return qry(rs,ql,qr);
return qry(ls,ql,qr)*qry(rs,ql,qr);
} int main() {
scanf("%d%d%s", &n, &q, s+1);
build(1,1,n);
REP(i,1,q) {
int l, r;
scanf("%d%d", &l, &r);
int ans = qry(1,1,n,l,r).v[0][4];
printf("%d\n", ans==INF?-1:ans);
}
}
New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)的更多相关文章
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- hdu 4576(简单概率dp | 矩阵优化)
艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle? 感觉很不公平.大家算法都一致,因为我程序没有那 ...
- CF1151F Sonya and Informatics (计数dp+矩阵优化)
题目地址 Solution (duyi是我们的红太阳) (这里说一句:这题看上去是一个概率dp,鉴于这题的概率dp写法看上去不好写,我们其实可以写一个计数dp) 首先拿到这个题目我们要能设出一个普通d ...
- Consecutive Subsequence CodeForces - 977F (map优化DP)·
You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...
- Codeforces 319C DP 斜率优化
题意:在一颗森林有n颗数,编号从1到n,第i棵树高度是a[i].有一个伐木工想要砍伐这片森林,它的电锯每次可以将树的高度减少1,然后就必须要充电,充电的代价是他已经砍倒的树种编号最大的那颗树的代价(b ...
- BZOJ 1009: [HNOI2008]GT考试(kmp+dp+矩阵优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 思路:真的是好题啊! 对于这种题目,很有可能就是dp,$f[i][j]$表示分析到第 ...
- bzoj 1009 DP 矩阵优化
原来的DP: dp[i][j]表示长度为i的合法串,并且它的长度为j的后缀是给定串的长度为j的前缀. 转移: i==0 dp[0][0] = 1 dp[0][1~m-1] = 0 i>=1 dp ...
- [POJ2778]DNA Sequence(AC自动机 + DP + 矩阵优化)
传送门 AC自动机加DP就不说了 注意到 m <= 10,所以模式串很少. 而 n 很大就需要 log 的算法,很容易想到矩阵. 但是该怎么构建? 还是矩阵 A(i,j) = ∑A(i,k) * ...
随机推荐
- linux 子shell subshell和函数
关于子shell, subshell 参考:http://blog.csdn.net/sosodream/article/details/5683515 系统引导时的进程为 "原始进程&qu ...
- Why database migrations?
https://flywaydb.org/getstarted/why First, let's start from the beginning and assume we have a proje ...
- powershell的stable和preview版本
在看https://github.com/PowerShell/PowerShell/releases的时候发现,已经发布了6.2.0的preview版本的情况下,还会继续发布6.1.3. 在Read ...
- vue 弹性布局 实现长图垂直居上,短图垂直居中
vue 弹性布局 实现长图垂直居上,短图垂直居中 大致效果如下图,只考虑垂直方向.长图可以通过滚动条看,短图居中效果,布局合理 html代码(vue作用域内): <div class=" ...
- 【Mybatis】-- Mapper动态代理开发注意事项
1.1. Mapper动态代理方式 1.1.1. 开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对 ...
- dhcp、tftp及pxe简介
DHCP: 全称:Dynamic Host Configuration Protocol 动态主机配置协议 DHCP配置内容: IP/Netmask Gateway DNS Server bootp ...
- 3、Python函数详解(0601)
回顾: re search,findall,finditer.sub,subn function () 调用函数 def func_name(arg1,....) 生成函数对象 func_s ...
- jQuery 知识点总结
jQuery 是一个“写的更少,但做的更多”的轻量级JavaScript 库.对于网页开发者来说,学会jQuery是必要的.因为它让你了解业界最通用的技术,为将来学习更高级的库打下基础,并且确实可以很 ...
- Linux命令之du命令
du命令 显示文件或目录所占用的磁盘空间. 命令格式: du [option] 文件/目录 -h 输出文件系统分区使用的情况,例如:10KB,10MB,10GB等 -s 显示文件或整个目录的大小,默认 ...
- Oracle imp导入数据
拿到别人给的dmp文件如何导入自己的库中呢?上码: 代码: imp dms/123321@orcl file=F:\TP_ZG_20171208.DMP feedback=10000 buffer=1 ...