CodeForces - 750E New Year and Old Subsequence 线段树优化DP/线段树维护矩阵

题目链接:

点击前往


题目:

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


题目大意:

给定只包含数字的字符串SS (4S200000)(4 \leq |S| \leq 200000),有q(1q200000)q(1 \leq q \leq 200000)次询问,每次询问给定区间[l,r][l,r],输出使得在这个区间内不含有子序列"2016"且含有子序列"2017"需要删除的最少的字符数,如果不能满足要求,输出1-1


解题思路:

我们可以把20172017拆成5种状态,分别如下:
,2,20,201,2017\emptyset , 2,20,201,2017
从前往后我们依次用0,1,2,3,40,1,2,3,4来代表这五种状态(如果字符串中存与对应状态相同的子序列,我们就称字符串处于状态i(i=0,1,2,3,4)i(i=0,1,2,3,4))。
那么,对于字符串中的每个字符,我们都可以用一个矩阵来表示删除这个字符带来的影响
比如,假设我们当前遍历到了第ii个字符SiS_i(设之前的i1i-1个字符组成的字符串为SS'),那么当Si=2S_i = 2时候,我们可以得到矩阵MMMijM_{ij}代表使得字符串SS'从状态ii变成字符串(S+SiS' + S_i)的状态jj,所需要删除的最少字符,若不可能则值为\infty)如下:
M=[100000] M = \left[ \begin{matrix} 1 & 0 & \infty & \infty & \infty \\ \infty & 0 & \infty & \infty & \infty \\ \infty & \infty & 0 & \infty & \infty \\ \infty & \infty & \infty & 0 & \infty \\ \infty & \infty & \infty & \infty & 0 \\ \end{matrix} \right]
具体解释一下,M00=1M_{00} = 1,因为从SS'的状态0变化到新串的状态0,就说明当前的字符22必须删掉,否则新串就会包含2,就变成了状态1。同理,M01=1M_{01} = 1,因为从SS'的状态0变化到新串的状态0,就说明当前的字符22必须保留,否则新串就不包含2,就变成了状态0。
M02=M_{02} = \infty,因为从SS'的状态0变化到新串的状态2,只添加当前字符22根本不可能达到。
同理,我们可以得到0170、1、7对应的矩阵,在这里就省略不写了。
要注意,因为不能包含2016,所以如果存在201,那么当前的6是必须要删掉的。矩阵如下:
M=[00011] M = \left[ \begin{matrix} 0 & \infty & \infty & \infty & \infty \\ \infty & 0 & \infty & \infty & \infty \\ \infty & \infty & 0 & \infty & \infty \\ \infty & \infty & \infty & 1 & \infty \\ \infty & \infty & \infty & \infty & 1 \\ \end{matrix} \right]
对于其他不等于201762、0、1、7、6中任何一个的字符,根本不会对产生影响,所以矩阵如下:
M=[] M = \left[ \begin{matrix} \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \end{matrix} \right]
那么,查询[l,r][l,r]之间的答案也就可以把这些矩阵合并起来得到了,具体见代码。


代码:

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
const int MAXN = 200000 + 100, MATSIZE = 5;
char s[MAXN];

struct Matrix {
    int a[MATSIZE][MATSIZE]{};

    Matrix() {
        memset(a, 0x3f, sizeof(a));
    }

    Matrix operator*(const Matrix &tMat) const {
        Matrix ans;
        for (int k = 0; k < MATSIZE; k++)
            for (int i = 0; i < MATSIZE; i++)
                for (int j = 0; j < MATSIZE; j++)
                    ans.a[i][j] = min(a[i][k] + tMat.a[k][j], ans.a[i][j]);
        return ans;
    }
};

struct SegTree {
    int l{}, r{};
    Matrix mat;
} T[MAXN << 2];

void build(int l, int r, int cur) {
    T[cur].l = l, T[cur].r = r;
    if (l == r) {
        for (int i = 0; i < MATSIZE; i++) T[cur].mat.a[i][i] = 0;
        if (s[l] == '2') T[cur].mat.a[0][0] = 1, T[cur].mat.a[0][1] = 0;
        if (s[l] == '0') T[cur].mat.a[1][1] = 1, T[cur].mat.a[1][2] = 0;
        if (s[l] == '1') T[cur].mat.a[2][2] = 1, T[cur].mat.a[2][3] = 0;
        if (s[l] == '7') T[cur].mat.a[3][3] = 1, T[cur].mat.a[3][4] = 0;
        if (s[l] == '6') T[cur].mat.a[3][3] = 1, T[cur].mat.a[4][4] = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, cur << 1);
    build(mid + 1, r, cur << 1 | 1);
    T[cur].mat = T[cur << 1].mat * T[cur << 1 | 1].mat;
}

Matrix query(int l, int r, int cur) {
    if (l <= T[cur].l && T[cur].r <= r) return T[cur].mat;
    if (l >= T[cur << 1 | 1].l) return query(l, r, cur << 1 | 1);
    if (r <= T[cur << 1].r) return query(l, r, cur << 1);
    return query(l, r, cur << 1) * query(l, r, cur << 1 | 1);
}

int main() {
    int n, m, l, r;
    Matrix tempMat;
    scanf("%d %d %s", &n, &m, s + 1);
    build(1, n, 1);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &l, &r);
        tempMat = query(l, r, 1);
        printf("%d\n", tempMat.a[0][4] <= n ? tempMat.a[0][4] : -1);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章