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 "2016" doesn't occur in t as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice.
The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is - 1.
Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).
The first line of the input contains two integers n and q (4 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the length of the string s and the number of queries respectively.
The second line contains a string s of length n. Every character is one of digits '0'–'9'.
The i-th of next q lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ n), describing a substring in the i-th query.
For each query print the ugliness of the given substring.
8 3
20166766
1 8
1 7
2 8
4
3
-1
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
-1
2
1
-1
-1
4 2
1234
2 4
1 2
-1
-1
In the first sample:
- In the first query, ugliness("20166766") = 4 because all four sixes must be removed.
- In the second query, ugliness("2016676") = 3 because all three sixes must be removed.
- In the third query, ugliness("0166766") = - 1 because it's impossible to remove some digits to get a nice string.
In the second sample:
- In the second query, ugliness("01201666209167") = 2. It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice.
- In the third query, ugliness("016662091670") = 1. It's optimal to remove the last digit '6', what gives a nice string "01666209170".
题目大意
给定一个长度为$n$的数字串。每次询问一个区间至少要删除多少个数字使得包含子序列"2017"但不包含子序列"2016",无解输出-1。
dp是显然的。
因为每次询问一个区间,所以需要把dp状态扔到某个数据结构上。先考虑线段树。
线段树更新的时候是拿两段的信息合并,所以不能像做1~n的dp那样记录状态。
考虑2017之间的间隔:
| 2 | 0 | 1 | 7 |
0 1 2 3 4
线段树的每个节点存一个矩阵$A$。$a_{ij}$表示使原串的子序列包含2017中第$i$个间隔到第$j$个间隔组成的子串,但不包含严格包含它的子序列最少需要删除的数字、
转移是显然的,和区间dp一样。枚举区间,枚举中间点,然后转移就好了。
考虑初值问题,显然的是非2、0、1、7、6的数字对答案不影响,所以令$a_{ii} = 0$,$a_{ij} = \infty \ \ \ \left ( i \neq j \right )$。
考虑当前数字是2的时候,如果我希望只包含子串$[0, 0]$(这里表示两个间隔间的子串),那么就必须删掉这个2,故$a_{00} = 1$,如果希望包含子串$[0, 1]$,那么什么都不用做,所以$a_{01} = 0$。对于0、1、7同理。
考虑当前数字是6的时候,那么遇到子串$[i, 3]$希望转移回自己,那么需要付出1的代价,因为否则会包含子序列"2016",同样如果遇到子串$[i, 4]$希望转移回自己,那么也需要付出1的代价。
由于很早以前过的这道题,所以不想重写一份,代码有点丑,请谅解。
Code
/**
* Codeforces
* Problem#750E
* Accepted
* Time: 998ms
* Memory: 49276k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<malloc.h>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) return;
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} typedef class Matrix {
public:
int mat[][]; //0: 1:2 2:20 3:201 4:2017
Matrix(){ }
Matrix(char x){
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
mat[i][j] = (i == j) ? () : (inf);
if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
} Matrix operator +(Matrix x) {
Matrix res;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++){
res.mat[i][j] = inf;
for(int k = ; k < ; k++)
smin(res.mat[i][j], mat[i][k] + x.mat[k][j]);
}
return res;
}
}Matrix; typedef class SegTreeNode {
public:
Matrix a;
SegTreeNode* left, *right;
SegTreeNode():left(NULL), right(NULL){ }
SegTreeNode(char x):left(NULL), right(NULL){
a = Matrix(x);
} void pushUp() {
a = left->a + right->a;
}
}SegTreeNode; typedef class SegTree {
public:
SegTreeNode* root;
SegTree():root(NULL){ }
SegTree(int size, char* str){
build(root, , size, str);
} void build(SegTreeNode*& node, int l, int r, char* list){
if(l == r){
node = new SegTreeNode(list[l]);
return;
}
node = new SegTreeNode();
int mid = (l + r) >> ;
build(node->left, l, mid, list);
build(node->right, mid + , r, list);
node->pushUp();
} Matrix query(SegTreeNode*& node, int l, int r, int from, int end) {
if(l == from && r == end) return node->a;
int mid = (l + r) >> ;
if(end <= mid) return query(node->left, l, mid, from, end);
if(from > mid) return query(node->right, mid + , r, from, end);
return query(node->left, l, mid, from, mid) + query(node->right, mid + , r, mid + , end);
}
}SegTree; int n, q;
char* str;
SegTree st; inline void init() {
readInteger(n);
readInteger(q);
str = new char[(const int)(n + )];
scanf("%s", str + );
st = SegTree(n, str);
} inline void solve() {
int a, b;
while(q--) {
readInteger(a);
readInteger(b);
Matrix c = st.query(st.root, , n, a, b);
printf("%d\n", (c.mat[][] == inf) ? (-) : (c.mat[][]));
}
} int main() {
init();
solve();
return ;
}
Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划的更多相关文章
- 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
Description 题库链接 给出一个长度为 \(n\) 的仅包含数字的字符串. \(q\) 次询问,每次询问该串 \([a,b]\) 段内删去几个数能够使其不含 \(2016\) 的子串,但存在 ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- CodeForces 587 E.Duff as a Queen 线段树动态维护区间线性基
https://codeforces.com/contest/587/problem/E 一个序列, 1区间异或操作 2查询区间子集异或种类数 题解 解题思路大同小异,都是利用异或的性质进行转化,st ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- Codeforces 629D Babaei and Birthday Cakes DP+线段树
题目:http://codeforces.com/contest/629/problem/D 题意:有n个蛋糕要叠起来,能叠起来的条件是蛋糕的下标比前面的大并且体积也比前面的大,问能叠成的最大体积 思 ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
随机推荐
- C#使用HttpWebRequest与HttpWebResponse模拟用户登录
模拟艺龙旅游网登录 想模拟登录,首先整理一下流程 1.通过360浏览器(IE,火狐等等)F12开发人员工具抓到相关数据 2.获取验证码(拿到cookie),登录时也需要使用 3.登录 -------- ...
- 48.HTML---Flex 布局教程:实例篇
你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇>.我的主要参考资料是Landon Schropp的文章和Solve ...
- MyBatis基础入门《五》核心配置文件
MyBatis基础入门<五>核心配置文件 描述: 在前面的章节中,简单的学习使用了一下mybatis,对于配置文件没有过多详细说明. 这里先描述项目中的一个核心配置文件:mybatis-c ...
- 前端forEach在Array、map、set中的使用,weakset,weakmap
数组: var s = ['a','b','c']; s.forEach(function(ele,index,array){ console.log(ele); }); Map: var map = ...
- shell作业控制(后台前台命令)
ctrl+z暂停命令(任务) fg调回命令 | fg +id号 bg放在后台持续执行 vmstat 1 & 在后面加上‘&’ 即相当于bg jobs列出当前的 ...
- windows假装更新升级
http://fakeupdate.net/ 进入这个网站,选择一款系统界面,按F11进去全屏 比较有趣
- c扩展开发
为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍.IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显优势. 另外C扩 ...
- java三大工厂结果总览
2018-11-02 21:27:18 开始写 谢谢.Thank you.Salamat Do(撒拉玛特朵).あリがCám o*n(嘉蒙)とゥ(阿里嘎都).감사합니다 (勘三哈咪瘩).terima K ...
- 【转】win中IDLE选择virtualenv的启动方法
从dos命令行运行.(virtualenv dir)\Scripts\activate.bat脚本激活环境,然后执行: python -m idlelib.idle 摘录:https://blog.c ...
- EasyUI表格DataGrid前端分页和后端分页的总结
Demo简介 Demo使用Java.Servlet为后台代码(数据库已添加数据),前端使用EasyUI框架,后台直接返回JSON数据给页面 1.配置Web.xml文件 <?xml version ...