题目大意

  给定一个长度为\(n\)的数字序列\(s\)\(1 \leq n \leq 100000\)),满足\(1 \leq s_i \leq q\)\(5 \leq q \leq 9\))。求一个最短的子序列\(s'\),满足\(s'\)\(s\)中没有出现。为了方便,请输出\(s'\)的长度。

题解

  当存在一个\(i\),满足\(s_1 \dots s_i\)中数字\(1 \dots q\)都出现过,那么肯定不存在长度\(\leq 1\)\(s'\)。当存在一个\(j\),满足\(s_{i + 1} \dots s_j\)中数字\(1 \dots q\)都出现过,那么肯定不存在长度\(\leq 2\)\(s'\)
  容易推广得对于序列\(p\),如果满足\(p_0 = 1\)\(p_i < p_{i + 1}\)\(p_m \leq n\),且\(s_{p_i} \dots s_{p_{i + 1} - 1}\)中数字\(1 \dots q\)都出现过,则最大的\(m\)即为所求的\(s'\)的长度。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n, q;
bool b[15];
int ans = 1;

int main()
{
    scanf("%d%d", &n, &q);
    int cnt = 0, tmp;
    while (n--)
    {
        getchar();
        tmp = getchar() - '0';
        if (!b[tmp])
        {
            b[tmp] = true;
            ++cnt;
            if (cnt == q)
            {
                ++ans;
                cnt = 0;
                memset(b, 0, sizeof b);
            }
        }
    }
    printf("%d", ans);
    return 0;
}
01-14 17:57
查看更多