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. Beta冲刺 第六天

    Beta冲刺 第六天 1. 昨天的困难 1.对于设计模式的应用不熟悉,所以在应用上出现了很大的困难. 2.SSH中数据库的管理是用HQL语句实现的,所以在多表查询时出现了很大的问题. 3.页面结构太凌 ...

  2. 201621123068 作业07-Java GUI编程

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 注册.事件.事件源.监听 1.2 任意编写事件处理相 ...

  3. Scrum 冲刺 第二日

    Scrum 冲刺 第二日 目录 要求 项目链接 燃尽图 问题 今日任务 明日计划 成员贡献量 要求 各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图:如 ...

  4. 400多个开源项目以及43个优秀的Swift开源项目-Swift编程语言资料大合集

    Swift 基于C和Objective-C,是供iOS和OS X应用编程的全新语言,更加高效.现代.安全,可以提升应用性能,同时降低开发难度. Swift仍然处于beta测试的阶段,会在iOS 8发布 ...

  5. var 和 let 的异同?

    相同点 声明后未赋值表现一致 不同点 1.使用未声明的变量表现不同 2.变量作用范围不同 3.var可以声明多次 let只能声明一次 let的好处就是当我们在写代码的时候可以避免在不知道的情况下重复声 ...

  6. JAVA_SE基础——3.Java程序的开发流程

    上一篇,写的是JAVA的环境变量的配置,今天我抽空写篇Java程序的开发流程,下面的教程是我结合书本和毕向东老师的视频写下的心的~ 在没有真正写Java程序前,首先需要了解Java程序的开发过程. S ...

  7. LeetCode & Q122-Best Time to Buy and Sell Stock II-Easy

    Description: Say you have an array for which the ith element is the price of a given stock on day i. ...

  8. js进度条小事例

    <style> #div1{width: 500px;height: 20px;border: 1px solid gray;} #div2{height: 20px;width: 0px ...

  9. 用python实现与小米网关通讯

    python 与小米网关通讯的三块内容: 以下内容的理解需要配合<绿米网关局域网通讯协议>使用 1.监听网关发出的组播信息:(有网关及连接设备的生命信号,事件信息) 2.读取需要获得的信息 ...

  10. 第四章 使用jQuery操作DOM

    第四章 使用jQuery操作DOM 一.DOM操作 在jQuery中的DOM操作主要可分为样式操作.文本和value属性值操作.节点操作: 节点操作又包含属性操作.节点遍历和CSS-DOM操作. 其中 ...