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的更多相关文章

  1. Codeforces Round #452 (Div. 2) A B C

    Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...

  2. Codeforces Round #452 (Div. 2) 899E E. Segments Removal

    题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...

  3. 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 ...

  4. Codeforces Round #452 F. Letters Removing

    Description Petya has a string of length n consisting of small and large English letters and digits. ...

  5. 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 ...

  6. Codeforces Round #452 (Div. 2)

    第一次打..(太弱(+99积分是几个意思 A 题意:一堆数,只有1和2,问最多凑出多少个3. 分情况即可 #include<cstdio> int main(){ int a=0,b=0, ...

  7. Codeforces Round #451 & Codeforces Round #452

    Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...

  8. 【Codeforces Round #452 (Div. 2) A】 Splitting in Teams

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心 1优先和2组队. 如果1没有了 就结束. 如果1还有多余的. 那么就自己3个3个组队 [代码] #include <bi ...

  9. 【Codeforces Round #452 (Div. 2) B】Months and Years

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 闰,平,平 平,闰,平 平,平,闰 平,平,平 4种情况都考虑到就好. 可能有重复的情况. 但是没关系啦. [代码] #includ ...

随机推荐

  1. 20162302 实验一《Java开发环境的熟悉》实验报告

    实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:Java开发环境的熟悉 实验器材:装有Ubuntu的联想拯救者80RQ 实验目的与要求:1.使 ...

  2. 解决java.lang.NoSuchMethodError:org.joda.time.DateTime.withTimeAtStartOfDay() Lorg/joda/time/DateTime

    问题:项目放在weblogic运行,报错 java.lang.NoSuchMethodError: org.joda.time.DateTime.withTimeAtStartOfDay()Lorg/ ...

  3. SQLite 带你入门

    SQLite数据库相较于我们常用的Mysql,Oracle而言,实在是轻量得不行(最低只占几百K的内存).平时开发或生产环境中使用各种类型的数据库,可能都需要先安装数据库服务(server),然后才能 ...

  4. oracle导入命令,记录一下

    工作中用到了,这个命令,记录一下,前提要安装imp.exe imp PECARD_HN/PECARD_HN@127.0.0.1:1521/orcl file=E:\work\dmp\PECARD_HN ...

  5. java中类的三大特征之多态

    Java 多态 同一种事物由于条件不同,展示出不同的结果,叫做多态. 父类的引用类型,由于使用不同的子类对象实例,而执行不同的操作. 多态存在的三个必要条件 1. 子类继承父类: 2. 子类重写父类方 ...

  6. LXC学习实践(2)安装LXC

    1.准备工作: yum install gcc yum install libcap-devel yum install libcgroup 2.安装LXC 下载源代码:sourceforge.net ...

  7. Docker学习笔记 - Docker的远程访问

    学习内容: 配置客户端与守护进程的远程访问 服务端配置-H选项: 使服务端支持远程被访问 客户端使用-H选项: 使客户端访问远程服务端 本地环境DOCKER_HOST设置客户端访问的默认服务端地址 准 ...

  8. 阿里云API网关(5)用户指南(调用 API)

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  9. MySql入门(2-2)创建数据库

    mysql -u root -p; show databases; create database apigateway; use apigateway; show tables;

  10. C# 后台构造json数据

    前后台传值一般情况下,都会用到json类型的数据,比较常见,但是每次用到的时候去网上找比较麻烦,所以自己记录一下,下次直接用. 构造的json串格式,如下: [{","name&q ...