题目大意:给定一个长度为 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的更多相关文章

  1. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  2. 【LeetCode】674. Longest Continuous Increasing Subsequence 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 空间压缩DP 日期 题目地址:https: ...

  3. 【HDOJ】1423 Greatest Common Increasing Subsequence

    LCIS /* 1423 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...

  4. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  5. 【二分答案nlogn/标解O(n)】【UVA1121】Subsequence

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  6. 【CF888E】Maximum Subsequence(meet in the middle)

    [CF888E]Maximum Subsequence(meet in the middle) 题面 CF 洛谷 题解 把所有数分一下,然后\(meet\ in\ the\ middle\)做就好了. ...

  7. 【CF888E】Maximum Subsequence 折半搜索

    [CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...

  8. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  9. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

随机推荐

  1. CentOS 7 安装java 环境

    1.创建安装目录 mkdir /usr/local/java/ 2.将下载的安装包 上传到 安装目录   (可用Xftp 上传) 3.解压 tar -xzvf jdk-8u221-linux-x64. ...

  2. VMware Workstation 15 Pro简化安装Kali Linux 2019.2

    记录下简单安装的步骤

  3. 如何将其它javaweb项目变成可以成功在自己eclipse环境中运行的javaweb项目?

    说明:此文档仅适用于以下两种情况     (1)myeclipse项目需要在eclipse环境中运行     (2)eclipse项目,但是无法在自己的电脑eclipse环境中运行     注意:以下 ...

  4. C#DataGridView格式化显示单元格的内容

    今天又发现了一个很有用的东西,DataGridView的CellFormating事件 经常从数据库查到的原始数据需要经过转换之后显示在客户端,比如性别,“1”显示“男”,“0”显示“女”,为此经常将 ...

  5. SpringMVC必备知识点汇总

    1.参数接收 1.1 数组 1.2 文件上传 1.2.1 单个文件上传 1.2.2 多个文件上传 1.2.3 文件上传时,携带其他数据 2.参数校验 参数校验:https://www.cnblogs. ...

  6. PostgreSQL unlogged表

    PostgreSQL有一种介于正常表和临时表之间的类型表,称之为unlogged表,在该表新建的索引也属于unlogged,该表在写入数据时候并不将数据写入到持久的write-ahead log文件中 ...

  7. CF949E Binary Cards 题解

    题面 首先发现:一个数最多会出现1次: 然后深入推出:一个数不会既用它又用它的相反数: 这样就可以依次考虑每一位了: 如果所有的数都不含有这一位,那么就直接把所有的数除以2 如果含有,那么就减去这一位 ...

  8. 实现Promise类

    基本使用: let promise = new Promise((resolve, reject) => { // do something if (true) { resolve('succe ...

  9. Js中toFixed()方法保留小数不精准的问题

    toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 问题:部分特殊数值使用toFixed() 方法会出现转换不正确的情况,举个例子: (3329.225).toFixed(2) ...

  10. SDX Instance Resource Assignment Guide 1 of 2

    SDX Instance Resource Assignment Guide 1 of 2 Memory and vCPU Requirements for NetScaler VPX https:/ ...