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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章