【CF750E】New Year and Old Subsequence
题目大意:给定一个长度为 N 的字符串,定义一个字串是“好的”,当且仅当字串中含有一个 “2017” 的子序列,且不含有 “2016” 的子序列。现给出 M 个询问,每次询问区间 [l, r] 内至少删去多少个字符才能使得该区间变成“好的”。
题解:
由于题目中要求的是子序列,且序列长度仅为 4,考虑状压,即:"" -> 0, “2” -> 1, "20" -> 2, "201" -> 3, "2017" -> 4。现假设只有一次询问的话,可以进行全局的一次 dp,状态为:dp[i][s] 表示到 i 下标为止,序列的状态是 s 需要删去的最小字符个数。可以发现转移方程仅与 i - 1 有关,又考虑到要回答区间 [l, r] 的询问,可以采用线段树维护矩阵乘法的形式。
代码如下
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
char s[maxn];
int n, m;
struct matrix {
int mat[5][5];
matrix() {
memset(mat, 0x3f, sizeof(mat));
}
int *operator[](int x) {
return mat[x];
}
friend matrix operator*(matrix &x, matrix &y) {
matrix z;
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
for (int k = 0; k <= 4; k++) {
z[i][j] = min(z[i][j], x[i][k] + y[k][j]);
}
}
}
return z;
}
};
struct node {
#define ls(o) t[o].lc
#define rs(o) t[o].rc
int lc, rc;
matrix mat;
} t[maxn << 1];
int tot, rt;
inline void pull(int o) {
t[o].mat = t[ls(o)].mat * t[rs(o)].mat;
}
void build(int &o, int l, int r) {
o = ++tot;
if (l == r) {
for (int i = 0; i < 5; i++) t[o].mat[i][i] = 0;
if (s[l] == '2') t[o].mat[0][1] = 0, t[o].mat[0][0] = 1;
if (s[l] == '0') t[o].mat[1][2] = 0, t[o].mat[1][1] = 1;
if (s[l] == '1') t[o].mat[2][3] = 0, t[o].mat[2][2] = 1;
if (s[l] == '7') t[o].mat[3][4] = 0, t[o].mat[3][3] = 1;
if (s[l] == '6') t[o].mat[3][3] = 1, t[o].mat[4][4] = 1;
return;
}
int mid = l + r >> 1;
build(ls(o), l, mid);
build(rs(o), mid + 1, r);
pull(o);
}
matrix query(int o, int l, int r, int x, int y) {
if (l == x && r == y) {
return t[o].mat;
}
int mid = l + r >> 1;
if (y <= mid) {
return query(ls(o), l, mid, x, y);
} else if (x > mid) {
return query(rs(o), mid + 1, r, x, y);
} else {
matrix ansl = query(ls(o), l, mid, x, mid);
matrix ansr = query(rs(o), mid + 1, r, mid + 1, y);
return ansl * ansr;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> s + 1;
build(rt, 1, n);
while (m--) {
int l, r;
cin >> l >> r;
int ans = query(rt, 1, n, l, r)[0][4];
cout << (ans == inf ? -1 : ans) << endl;
}
return 0;
}
【CF750E】New Year and Old Subsequence的更多相关文章
- 【Leetcode_easy】674. Longest Continuous Increasing Subsequence
problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...
- 【LeetCode】674. Longest Continuous Increasing Subsequence 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 空间压缩DP 日期 题目地址:https: ...
- 【HDOJ】1423 Greatest Common Increasing Subsequence
LCIS /* 1423 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【二分答案nlogn/标解O(n)】【UVA1121】Subsequence
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- 【CF888E】Maximum Subsequence(meet in the middle)
[CF888E]Maximum Subsequence(meet in the middle) 题面 CF 洛谷 题解 把所有数分一下,然后\(meet\ in\ the\ middle\)做就好了. ...
- 【CF888E】Maximum Subsequence 折半搜索
[CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents...
出现这个错误的原因是时区有问题,可以在mysql中执行命令: set global time_zone='+8:00'; 如上是修改为北京时间(GMT+0800). 查看修改: show variab ...
- 服务间的通信 RestTemplate和Feign
1.RestTemplate Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够 ...
- comodo firewall 科莫多离线安装
comodo firewall是什么?他配有HIPS,配置好规则就可以比杀软强不是一个两个档次,但是新手不建议使用. 注意:不用使用疯狂模式后锁屏,不然系统都打不开. 下载地址: https://do ...
- BUUOJ reverse 不一样的flag
不一样的flag 是不是做习惯了常规的逆向题目?试试这道题,看你在能不能在程序中找到真正的flag!注意:flag并非是flag{XXX}形式,就是一个’字符串‘,考验眼力的时候到了! 注意:得到的 ...
- superset连接sqlite频繁断开
出现上述现象的原因是SQLite只支持库级锁,不支持并发执行写操作,即使是不同的表,同一时刻也只能进行一个写操作.例如,事务T1在表A新插入一条数据,事务T2在表B中更新一条已存在的数据,这两个操作是 ...
- kafka整理笔记笔记
一.为什么需要消息系统 解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险.许多消息 ...
- Ubuntu中配置Python虚拟环境Virtualenv
Ubuntu版本为18.04 Virtualenv介绍 在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-pac ...
- bash 转换为C代码
bash 转换为C代码,并编译为可执行文件 [root@localhost ~]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9. ...
- 附录1:arrayanalysis的本地使用(质量控制)
访问:https://github.com/BiGCAT-UM/affyQC_Module,点击“Download ZIP”,下载得到affyQC_Module-master.zip,解压得到一个af ...
- ArrayList扩容分析
一段java代码 String e = "q3234v"; List<String> list = new ArrayList<String>(); for ...