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 ...
随机推荐
- @AfterThrowing
@AfterThrowing(throwing="ex", pointcut="within(com.xms.controller.*)") public vo ...
- html网页什么样的字体最好看,css设置各种中文字体样式代码
css代码如下:{ font-family:"Microsoft YaHei",微软雅黑,"MicrosoftJhengHei",华文细黑,STHeiti,Mi ...
- CSU 1849 Comparing answers(数学矩阵)
Comparing answers 离散数学真的要好好学啊:一个邻接矩阵(这个矩阵一定是n×n的方阵,n是图的节点个数),表示的是从i到j有几条通路的时候,矩阵的1次方就代表从从i到j长度为1的路径通 ...
- python时间和日期
一.time 和 calendar 模块可以用于格式化日期和时间 import time; # 引入time模块 ticks = time.time() print "当前时间戳为:&quo ...
- hessian 在spring中的使用 (bean 如 Dao无法注入的问题)
hessian的主要结构分客户端与服务端,中间基于http传输.客户端主要做的事情是把对远程接口调用序列化为流,并传输到服务端:服务端主要做的事情是把传输过来的流反序列化为对服务的请求,调用相应服务后 ...
- 关于linux下firefox无法播放mp3文件
起因是发现在线翻译的网站没法发音. F12调出console,发现是没法解码mp3文件. 尝试安装deadbeef包,发现安装之后推荐安装的软件列表中有个名为ffmpeg的包.安装上之后,重启fire ...
- linux文本文件编辑命令
1.cat命令 cat命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]”. Linux系统中有多个用于查看文本内容的命令,每个命令都有自己的特点,比如这个cat命令就是用于查看 ...
- ps 证件照(1,2寸)
制作证件照 9*9打印 1,1寸 图片裁剪 2, 2寸 图片裁剪 3,将裁剪完成后的图片选择添加画布 Alt Ctrl c 将高和宽各加20px ,背景选择白色 4,将得到的带有白色 ...
- Python全栈-day12-day13-函数4
1.迭代器 1)定义:迭代取值的工具 2)优缺点 优点 a.提供一种不依赖索引取值的方法 b.同时一时刻内存在存在的值只有一个,更加省内存 缺点 a.取值麻烦,只能通过next方法一个一个地往后取 b ...
- Maven的配置指南
Maven的配置指南 配置Maven Maven配置发生在3个级别: 项目 - 大多数静态配置发生在pom.xml中 安装 - 这是Maven安装时发生的一次性的配置过程 用户 - 这是Maven提 ...