【DP+线段树转移矩阵】2019南昌ICPC网络赛 C. Hello 2019

https://nanti.jisuanke.com/t/41350

A digital string is "good": when it contains a subsequence 91029102 and does not contain a subsequence 81028102.

The bad value of a string is defined as how many characters are to remove at least, so that the string satisfies the "good" property. Output -1 if the string cannot satisfy the "good" property by removing some characters (0 or maybe more).

Input

The first line contains two integers n, Qn,Q(1\leq n,Q\leq2*10^5)(1≤n,Q≤2∗105). Where nn is the length of the string and QQ is the number of queries.

The second line contains a string ss that consists entirely of decimal numbers.

The next QQ line, each line contains two integers l, rl,r(1\leq l\leq r\leq n)(1≤l≤r≤n), denoting a query.

Output

For each query, output an answer which is the bad value of the substring s_ls_{l+1} \cdots s_rsl​sl+1​⋯sr​ from ss.

样例输入复制

8 3
88988102
1 8
2 8
1 7

样例输出复制

4
3
-1

 

#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
const int maxn=2e5+10;
struct node
{
    int val[5][5];
    node() //新建初始化
    {
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                val[i][j]=INF;
    }

    node operator + (node x) //重载运算符 矩阵相加
    {
        node ans;
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                for(int k=0;k<5;k++)
                    ans.val[i][j]=min(ans.val[i][j],val[i][k]+x.val[k][j]);
        return ans;
    }
}tree[maxn*4];

char ch[10]={'2','0','1','9','8'};
char s[maxn],S[maxn];

node pushup(node &a,node &b)
{
    node res;
    for(int i=0;i<5;i++)
        for(int j=i;j<5;j++)
            for(int k=i;k<=j;k++)
                res.val[i][j]=min(res.val[i][j],a.val[i][k]+b.val[k][j]);
    return res;
}

int Find(char c)
{
    for(int i=0;i<5;i++)
    {
        if(ch[i]==c)
            return i;
    }
    return -1;
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        int t=Find(s[l]);
        for(int i=0;i<5;i++)
        {
            tree[rt].val[i][i]=0;
        }
        if(t!=-1&&t<4) //是2017中的
        {
            tree[rt].val[t][t+1]=0;
            tree[rt].val[t][t]=1;
        }
        else if(t==4) //有6
        {
            tree[rt].val[3][3]=tree[rt].val[4][4]=1;
        }
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,2*rt);
    build(mid+1,r,2*rt+1);
    tree[rt]=pushup(tree[2*rt],tree[2*rt+1]);
}

node query(int l,int r,int rt,int L,int R)
{
    if(L<=l&&R>=r)
    {
        return tree[rt];
    }
    int mid=(l+r)/2;
    if(mid<L) return query(mid+1,r,2*rt+1,L,R);
    if(mid>=R) return query(l,mid,2*rt,L,R);
    return query(l,mid,2*rt,L,R)+query(mid+1,r,2*rt+1,L,R); //注意一定要左区间在前,右区间在后,不然答案会不对
}


int main()
{
    int n,m,l,r;
    scanf("%d%d%s",&n,&m,s+1);
    int len=n;
    for(int i=1;i<=n;i++)
    {
        S[i]=s[len--];
    }
    for(int i=1;i<=n;i++)
    {
        s[i]=S[i];
    }
    build(1,n,1);
    while(m--)
    {
        node ans;
        scanf("%d%d",&l,&r);
        int ql=n-r+1,qr=n-l+1;
        l=ql,r=qr;
        ans=query(1,n,1,l,r);
        if(ans.val[0][4]==INF)
            printf("-1\n");
        else
            printf("%d\n",ans.val[0][4]);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值