题目大意:给定一个长度为 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. eclipse中出现错误 Syntax error, insert "}" to complete Block

    结果原因如下 划红线的地方多出来类似于空格的占位符,也许这样看不清楚. 我们来显示空格.制表符和回车键. Window->Preferences->General->Editors- ...

  2. webdriervAPI(By声明定位方法)

    from  selenium  import  webdriver from  selenium.webdriver.common.by  import  By 导入By方法 driver  =  w ...

  3. finereport 填报 单元格 JS 触发 提交SQL 事件

    var location = this.options.location; var cr = FR.cellStr2ColumnRow(location); var col = cr.col; var ...

  4. WCf客户端测试

    添加项目ConsoleWCFTest 添加WCFService.WCFServiceProxy 配置App.config <?xml version="1.0" encodi ...

  5. 论文阅读 | BadNets: Identifying Vulnerabilities in the Machine Learning Model Supply Chain

    BadNets: 识别机器学习模型供应链中的漏洞 摘要 基于深度学习的技术已经在各种各样的识别和分类任务上取得了最先进的性能.然而,这些网络通常训练起来非常昂贵,需要在许多gpu上进行数周的计算;因此 ...

  6. pikachu-SQL注入

    参考网址: http://www.mamicode.com/info-detail-2795438.html

  7. windows10 AppStore安装 应用商店重新安装

    点击左下角的搜索按钮,如下图所示   输入powershell,在结果中找到widows powershell应用,如下图所示   右键单击widows powershell应用,选择以管理员运行,如 ...

  8. 两两内积为0(牛客多校第七场)-- CDMA

    题意: 构造一个n*n的矩阵,元素只能是-1或1,任意两行内积为0(两两相乘加起来和为0). 思路: #define IOS ios_base::sync_with_stdio(0); cin.tie ...

  9. 从入门到自闭之Python内置函数

    内置函数一 eval:执行字符串类型的代码 exac:执行字符串社类型的代码 eval与exac 禁止使用 hash()作用就是区分可变数据类型与不可变数据类型 # print(hash(" ...

  10. Zabbix 系统概述与部署

    Zabbix是一个非常强大的监控系统,是企业级的软件,来监控IT基础设施的可用性和性能.它是一个能够快速搭建起来的开源的监控系统,Zabbix能监视各种网络参数,保证服务器系统的安全运营,并提供灵活的 ...