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 ...
随机推荐
- 获取 Google USB 驱动程序
获取 Google USB 驱动程序 另请参阅 安装 USB 驱动程序 使用硬件设备 使用任何 Google Nexus 设备进行 ADB 调试时,只有 Windows 需要 Google ...
- EF There is already an open DataReader associated with this Command
捕捉到 System.InvalidOperationException _HResult=-2146233079 _message=意外的连接状态.在使用包装提供程序时,请确保在已包装的 DbCon ...
- Selenium基本使用(十三)测试中常见问题
我们在使用selenium测试过程中,经常会遇到这样的问题: 1.frame或iframe <iframe id='frame1' name='frame1'> <input typ ...
- MongoDB 在 windows 下的安装与服务配置
本文转载地址: https://blog.csdn.net/Dorma_Bin/article/details/80851230 本地安装及网页测试 在官网下载最新的安装文件 下载地址 : https ...
- shadow一键安装
https://blog.csdn.net/qq_4278923/article/details/80909686
- vue框架(三)_vue引入jquery、bootstrap
一.vue安装jquery 1.按照之前博客的内容,新建一个vue工程. 2.在项目文件夹下,使用命令npm install jquery --save-dev 引入jquery. 3.在build/ ...
- 导航,头部,CSS基础
1.制作自己的导航条. 2.HTML头部元素: <base> 定义了页面链接标签的默认链接地址 <style> 定义了HTML文档的样式文件 <link> 定 ...
- 【Hive学习之四】Hive 案例
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-3.1.1 ...
- Spring tokenizeToStringArray
tokenizeToStringArray: StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimToken ...
- 在Hue中提交oozie定时任务
可以参见下面这篇博文: 通过hue提交oozie定时任务