Codeforces Round #452 E. New Year and Old Subsequence
Description
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).
Solution
考虑朴素DP,记录\(2\),\(20\),\(201\),\(2017\)之间相互转移的最小代价,复杂度 \(O(N*Q)\)
这个过程可以矩乘优化,\(a[i][j]\)表示状态 \(i\) 转移到 状态\(j\)的代价,线段树维护区间答案即可
#include <bits/stdc++.h>
#define ls (o<<1)
#define rs (o<<1|1)
using namespace std;
const int N=200005,inf=2e8;
int n,Q;char s[N];
struct node{
int a[5][5];
node(){memset(a,0,sizeof(a));}
node operator *(const node &p){
node ret;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++){
ret.a[i][j]=inf;
for(int k=0;k<5;k++)
ret.a[i][j]=min(ret.a[i][j],a[i][k]+p.a[k][j]);
}
return ret;
}
}tr[N<<2];
inline void build(int l,int r,int o){
if(l==r){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
tr[o].a[i][j]=(i==j?0:inf);
if(s[l]=='2')tr[o].a[0][0]=1,tr[o].a[0][1]=0;
else if(s[l]=='0')tr[o].a[1][1]=1,tr[o].a[1][2]=0;
else if(s[l]=='1')tr[o].a[2][2]=1,tr[o].a[2][3]=0;
else if(s[l]=='7')tr[o].a[3][3]=1,tr[o].a[3][4]=0;
else if(s[l]=='6')tr[o].a[4][4]=1,tr[o].a[3][3]=1;
return ;
}
int mid=(l+r)>>1;
build(l,mid,ls);build(mid+1,r,rs);
tr[o]=tr[ls]*tr[rs];
}
inline node qry(int l,int r,int o,int sa,int se){
if(sa<=l && r<=se)return tr[o];
int mid=(l+r)>>1;
if(se<=mid)return qry(l,mid,ls,sa,se);
else if(sa>mid)return qry(mid+1,r,rs,sa,se);
else return qry(l,mid,ls,sa,mid)*qry(mid+1,r,rs,mid+1,se);
}
int main()
{
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
scanf("%d%d%s",&n,&Q,s+1);
build(1,n,1);
while(Q--){
int x,y;node t;
scanf("%d%d",&x,&y);
t=qry(1,n,1,x,y);
if(t.a[0][4]==inf)puts("-1");
else printf("%d\n",t.a[0][4]);
}
return 0;
}
Codeforces Round #452 E. New Year and Old Subsequence的更多相关文章
- Codeforces Round #452 (Div. 2) A B C
Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- Codeforces Round #452 (Div. 2)-899A.Splitting in Teams 899B.Months and Years 899C.Dividing the numbers(规律题)
A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #452 F. Letters Removing
Description Petya has a string of length n consisting of small and large English letters and digits. ...
- Codeforces Round #452 (Div. 2) C. Dividing the numbers(水)
C. Dividing the numbers Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in t ...
- Codeforces Round #452 (Div. 2)
第一次打..(太弱(+99积分是几个意思 A 题意:一堆数,只有1和2,问最多凑出多少个3. 分情况即可 #include<cstdio> int main(){ int a=0,b=0, ...
- Codeforces Round #451 & Codeforces Round #452
Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...
- 【Codeforces Round #452 (Div. 2) A】 Splitting in Teams
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心 1优先和2组队. 如果1没有了 就结束. 如果1还有多余的. 那么就自己3个3个组队 [代码] #include <bi ...
- 【Codeforces Round #452 (Div. 2) B】Months and Years
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 闰,平,平 平,闰,平 平,平,闰 平,平,平 4种情况都考虑到就好. 可能有重复的情况. 但是没关系啦. [代码] #includ ...
随机推荐
- ExecutorService,另一种服务,线程
http://heipark.iteye.com/blog/1393847 Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得 博客 ...
- 20145237《Java程序设计》第一周学习总结
教材学习内容总结 java可分为Java SE.Java EE.Java ME三大平台. java SE分为JVM.JRE.JDK.与java语言四个部分. JRE包括java SE API和JVM. ...
- bzoj 4399 魔法少女LJJ
4399: 魔法少女LJJ Time Limit: 20 Sec Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/problem.php?i ...
- 关于 Bootstrap的知识
Bootstrap是简单.灵活的用于搭建WEB页面的HTML.CSS.Javascript的工具集.Bootstrap基于HTML5和CSS3,具有漂亮的设计.友好的学习曲线.卓越的兼容性,还有12列 ...
- JAVA_SE基础——49.多态的应用
因为多态对以后开发的重要性,因此我在这里专门开个多态的应用来加深讲解,希望想弄懂多态的同学能耐心看完. 了解了对象多态性后,那么这多态到底有哪些用处了? 下面要求设计一个方法,要求此方法可以接受A类的 ...
- php代码一样,编码不同报错
php代码一样,编码不同报错 两个php代码完全一样,但是就报错,比如说声明比如在very first,这种,可以把编码设置utf-8 无bom
- Redux应用单一的store原则案例详解
在开发reac单页面应用的时候,页面的展示逻辑跟数据状态的关系管理变得越来越复杂,redux很好的解决这个问题.废话不多说,直接先上官网api链接. http://cn.redux.js.org/in ...
- windbg查找Kernel32.dll基址
一.首先准备好一个程序,运行起来,用windbg进行附加调试,由于每个windows下的程序都会加载kernel32.dll,因此,找基址的过程是一样的: 二.查看PEB地址: 法一.r $peb ...
- Spring Security入门(3-3)Spring Security 手工配置并注入 authenticationProvider 和 异常信息传递
特别注意的是 这样就能保证抛出UsernameNotFoundException时,前台显示出错信息: 另外,ps:
- django的models模型 关联关系和关系查询
模型类关系 关系字段类型 关系型数据库的关系包括三种类型: ForeignKey:一对多,将字段定义在多的一端中. ManyToManyField:多对多,将字段定义在两端中. OneToOneFie ...