[Codeforces 750E]New Year and Old Subsequence
Description
给出一个长度为 \(n\) 的仅包含数字的字符串。 \(q\) 次询问,每次询问该串 \([a,b]\) 段内删去几个数能够使其不含 \(2016\) 的子串,但存在 \(2017\) 的子串。
\(4\leq n\leq 200000,1\leq q\leq 200000\)
Solution
考虑朴素的 \(DP\) 。我们记 \(f_{i,j}\) 为匹配到 \(i\) 这个位置拼成 "" "2" "20" "201" "2017" 的最小花费。显然这个是 \(O(nq)\) 的。
但对于每一位的转移是独立的。所以我们考虑用矩乘优化,线段树维护。
Code
//It is made by Awson on 2018.2.7
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 200000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, q, a, b;
char ch[N+5];
struct mat {
int a[5][5];
mat() {memset(a, 127/3, sizeof(a)); }
mat(int **_a) {for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) a[i][j] = _a[i][j]; }
mat operator * (const mat &b) const {
mat ans;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
for (int k = 0; k < 5; k++)
ans.a[i][j] = Min(ans.a[i][j], a[i][k]+b.a[k][j]);
return ans;
}
};
struct Segment_tree {
mat sgm[(N<<2)+5];
#define lr(x) (x<<1)
#define rr(x) (x<<1|1)
void build(int o, int l, int r) {
if (l == r) {
for (int i = 0; i < 5; i++) sgm[o].a[i][i] = 0;
if (ch[l] == '2') sgm[o].a[0][0] = 1, sgm[o].a[0][1] = 0;
else if (ch[l] == '0') sgm[o].a[1][1] = 1, sgm[o].a[1][2] = 0;
else if (ch[l] == '1') sgm[o].a[2][2] = 1, sgm[o].a[2][3] = 0;
else if (ch[l] == '7') sgm[o].a[3][3] = 1, sgm[o].a[3][4] = 0;
else if (ch[l] == '6') sgm[o].a[3][3] = 1, sgm[o].a[4][4] = 1;
return;
}
int mid = (l+r)>>1;
build(lr(o), l, mid), build(rr(o), mid+1, r);
sgm[o] = sgm[lr(o)]*sgm[rr(o)];
}
mat query(int o, int l, int r, int a, int b) {
if (a <= l && r <= b) return sgm[o];
int mid = (l+r)>>1;
if (b <= mid) return query(lr(o), l, mid, a, b);
if (a > mid) return query(rr(o), mid+1, r, a, b);
return query(lr(o), l, mid, a, b)*query(rr(o), mid+1, r, a, b);
}
}T;
void work() {
read(n), read(q); scanf("%s", ch+1);
T.build(1, 1, n);
while (q--) {
read(a), read(b);
mat tmp = T.query(1, 1, n, a, b);
writeln(tmp.a[0][4] > n ? -1 : tmp.a[0][4]);
}
}
int main() {
work(); return 0;
}
[Codeforces 750E]New Year and Old Subsequence的更多相关文章
- Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划
A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)
大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数 转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全 ...
- Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...
- Codeforces Round #646 (Div. 2) B. Subsequence Hate(前缀和)
题目链接:https://codeforces.com/contest/1363/problem/B 题意 可以将 $01$ 串中的 $0$ 变为 $1$.$1$ 变为 $0$,问至少需要变换多少字符 ...
- Codeforces 750E 线段树DP
题意:给你一个字符串,有两种操作:1:把某个位置的字符改变.2:询问l到r的子串最少需要删除多少个字符,使得这个子串含有2017子序列,并且没有2016子序列? 思路:线段树上DP,我们设状态0, 1 ...
- Codeforces Round #646 (Div. 2) B. Subsequence Hate (思维,前缀和)
题意:给你一个只含有\(0\)和\(1\)的字符串,每次操作可以将\(0\)改成\(1\)或\(1\)改成\(0\),问最少操作多少次,使得子序列中不含有\(010\)和\(101\). 题解:仔细想 ...
随机推荐
- 网络1711-1712班 c 语言评分总表一览
学号 姓名 作业地址 PTA实验作业5分 PTA排名2分 阅读代码2分 总结1分 代码规范扣分-2--0 总分 是否推荐博客 1 **莹 http://www.cnblogs.com/wwwwxy12 ...
- 安利给班里的大家一个chrome的GitHub插件-----gayhub
title: 一个好用的Github插件--gayhub date: 2017-09-20 15:41:36 tags: --- 别跑, 这真是正经插件. 效果, 一图流: 具体效果在项目地址很详细 ...
- java8-Stream之数值流
在Stream里元素都是对象,那么,当我们操作一个数字流的时候就不得不考虑一个问题,拆箱和装箱.虽然自动拆箱不需要我们处理,但依旧有隐含的成本在里面.Java8引入了3个原始类型特化流接口来解决这个问 ...
- 超绚丽CSS3多色彩发光立方体旋转动画
CSS3添加了几个动画效果的属性,通过设置这些属性,可以做出一些简单的动画效果而不需要再去借助JavaScript.css3动画的属性主要分为三类:transform.transition以及anim ...
- SpringMVC源码情操陶冶#task-executor解析器
承接Spring源码情操陶冶-自定义节点的解析.线程池是jdk的一个很重要的概念,在很多的场景都会应用到,多用于处理多任务的并发处理,此处借由spring整合jdk的cocurrent包的方式来进行深 ...
- JAVA_SE基础——59.权限访问修饰符
了解了包的概念,就可以系统的介绍Java中的访问控制级别.在Java中,针对类.成员方法和属性提供了四种访问级别,分别是private.default.protected和public. 权限访问修饰 ...
- 深入理解java的static关键字
static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...
- machine learning 之 导论 一元线性回归
整理自Andrew Ng 的 machine learnig 课程 week1. 目录: 什么是机器学习 监督学习 非监督学习 一元线性回归 模型表示 损失函数 梯度下降算法 1.什么是机器学习 Ar ...
- Android开发——发布第三方库到JitPack上
前言: 看到大神们的写的第三方控件,比较好用,我们使用的时候直接是在gradle上加上代码就可以使用了,现在到我们写了一个第三方控件,想要别人使用的时候也是直接在gradle加上相关的代码就可以用了, ...
- JavaScript简单重写构造器的原型
//简单重写原型对象: //一个构造函数Person function Person(){ } //重写Person的原型 //把Person的原型赋值给一个新的对象 是我们重写的过程 Person. ...