【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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).
Input
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.
Output
For each query print the ugliness of the given substring.
Examples
input
8 3
20166766
1 8
1 7
2 8
output
4
3
-1
input
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
output
-1
2
1
-1
-1
input
4 2
1234
2 4
1 2
output
-1
-1
Note
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”.
【题目链接】:http://codeforces.com/contest/750/problem/E
【题解】
merge矩阵(dp)+线段树;
m[i][j]表示从状态i->j转移的花费;
0表示什么都没有
1表示出现了2
2表示出现了20
3表示出现了201
4表示出现了2017
出现了2,20,则遇到6的时候我么可以不用管;
但是如果出现了201或2017,然后又遇到了6,则我们需要把这个6删掉;
for (i = 0;i <= 4;i++)//除了遇到2,0,1,6,7这5个特别的数字之外,
m[i][i] = 0;//遇到的时候状态都不会变;所以不用花费(操作);
if (ch == '2')
{
m[0][0] = 1;//什么都没有->什么都没有,删掉这个2
m[0][1] = 0;//什么都没有->出现了2,花费变成0
//注意这里m[1][1] = 0,表示从出现了一个2到出现了一个2可以不用删掉任何东西,因为加上一个2也无妨,下面的解释就省略了,希望大家能看得明白.
}
if (ch=='0')
{
m[1][1] = 1 ;//出现了一个2->出现了一个2,则把0删掉
m[1][2] = 0;//从出现一个2->出现了20,因为当前就是0,所以什么都不用加
}
if (ch=='1')
{
ma[2][2] = 1;//出现了20->出现了20,则把1删掉
ma[2][3] = 0;//出现了20->出现了201,因为刚好遇到一个1则什么都不加
}
if (ch=='7')
{
m[3][3] = 1;//出现了201->出现了201,则把遇到的7删掉
m[3][4] = 0;//出现了201->出现了2017,刚好遇到7,则什么都不做
}
if (ch=='6')
{
a[3][3] = 1;//出现了201->出现了数字6,则一定要把6删掉,不然无法满足题意
a[4][4] = 1;//出现了2017->出现了数字6,则也一定要把6删掉.
}
线段树加一个合并操作就好;
那个合并的操作和floyd算法类似;
最后输出m[0][4];表示从什么都没有然后出现"2017"
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 2e5+10;
const int INF = 7e8;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
struct abc
{
int m[5][5];
friend abc operator * (abc a,abc b)
{
abc d;
rep1(i,0,4)
rep1(j,0,4)
{
d.m[i][j] = INF;
rep1(k,0,4)
d.m[i][j] = min(d.m[i][j],a.m[i][k]+b.m[k][j]);
}
return d;
}
};
int n,q;
abc a[MAXN*4];
char s[MAXN];
void build(int l,int r,int rt)
{
if (l==r)
{
char ch = s[l];
rep1(i,0,4)
rep1(j,0,4)
a[rt].m[i][j] = (i==j)?0:INF;
if (ch == '2')
{
a[rt].m[0][0] = 1;
a[rt].m[0][1] = 0;
}
if (ch=='0')
{
a[rt].m[1][1] = 1;
a[rt].m[1][2] = 0;
}
if (ch=='1')
{
a[rt].m[2][2] = 1;
a[rt].m[2][3] = 0;
}
if (ch=='7')
{
a[rt].m[3][3] = 1;
a[rt].m[3][4] = 0;
}
if (ch=='6')
{
a[rt].m[3][3] = 1;
a[rt].m[4][4] = 1;
}
return;
}
int m = (l+r)>>1;
build(lson);build(rson);
a[rt] = a[rt<<1]*a[rt<<1|1];
}
abc query(int L,int R,int l,int r,int rt)
{
if (L<=l && r <= R)
return a[rt];
int m = (l+r)>>1;
abc temp1,temp2;
bool f1 = 0,f2 = 0;
if (L <= m)
{
temp1 = query(L,R,lson);
f1 = 1;
}
if (m < R)
{
temp2 = query(L,R,rson);
f2 = 1;
}
if (f1&&f2)
return temp1*temp2;
if (f1)
return temp1;
if (f2)
return temp2;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(q);
scanf("%s",s+1);
build(1,n,1);
rep1(i,1,q)
{
int L,R;
rei(L);rei(R);
int ans = query(L,R,1,n,1).m[0][4];
if (ans>=INF)
puts("-1");
else
cout << ans << endl;
}
return 0;
}
【codeforces 750E】New Year and Old Subsequence的更多相关文章
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- 15、python学习手册之:python语句、赋值、表达式和打印
1.语句的另一个特殊规则是用一对括号把语句括起来就可以:括号().方括号[].字典的大括号{}.任何括在这些符号里的程序代码都可横跨好几行. 2.括号是可以包含一切的,因为任何表达式都可以包含在内,只 ...
- 【习题 6-1 UVA-673】Parentheses Balance
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 括号匹配. 栈模拟就好. 多种括号也是一样可以做的. [代码] #include <bits/stdc++.h> usi ...
- [Angular] HttpParams
It is possible to use HttpParams to set http params. For example we have this url to make: https://a ...
- OpenNI2获取华硕XtionProLive深度图和彩色图并用OpenCV显示
使用OpenNI2打开XtionProLive时有个问题,彩色图分辨率不管怎样设置始终是320*240,深度图倒是能够设成640*480,而OpenNI1.x是能够获取640*480的彩色图的. 彩色 ...
- 基于bootstrap的漂亮网站后台管理界面框架汇总
基于bootstrap的漂亮网站后台管理界面框架汇总 10个最新的 Bootstrap 3 管理模板 这里分享的 10 个模板是从最新的 Bootstrap 3 管理模板集合中挑选出来的,可以帮助你用 ...
- shrio 权限管理filterChainDefinitions过滤器配置(转)
shrio 权限管理filterChainDefinitions过滤器配置 /** * Shiro-1.2.2内置的FilterChain * @see ======================= ...
- Linux环境编程之共享内存区(一):共享内存区简单介绍
共享内存区是可用IPC形式中最快的.一旦内存区映射到共享它的进程的地址空间,进程间数据的传递就不再涉及内核.然而往该共享内存区存放信息或从中取走信息的进程间通常须要某种形式的同步.不再涉及内核是指:进 ...
- centos中的配置文件 分类: B3_LINUX 2015-04-03 22:21 184人阅读 评论(0) 收藏
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...
- PHP通用非法字符检测函数集锦
<? // [变量定义规则]:‘C_’=字符型,‘I_’=整型,‘N_’=数字型,‘L_’=布尔型,‘A_’=数组型 // ※CheckMoney($C_Money) 检查数据是否是 99999 ...
- PL/SQL精明的调用栈分析
PL/SQL精明的调用栈分析 原文:http://www.oracle.com/technetwork/issue-archive/2014/14-jan/o14plsql-2045346.html ...