luogu#P4792. [BalticOI 2018] 火星人的 DNA

    ID: 3815 远端评测题 2000ms 750MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>字符串2018枚举前缀和BalticOI(波罗的海)

[BalticOI 2018] 火星人的 DNA

Problem Description

This problem is translated from BalticOI 2018 Day 1 “Martian DNA”.

You are given a string of length NN over an alphabet of size Σ=K|\Sigma| = K, and RR requirements. Each requirement is: in the substring, character BB must appear at least QQ times. Find the length of the shortest substring that satisfies all requirements.

Input Format

The first line contains three integers N,K,RN,\, K,\, R, representing the length of the string, the size of the alphabet, and the number of requirements. It is guaranteed that 1RKN1\leqslant R\leqslant K\leqslant N.
The second line contains NN space-separated integers describing the string. Characters are numbered starting from 00, and every character in the alphabet appears at least once.
The next RR lines each contain two integers BB and QQ, describing one requirement. It is guaranteed that 0B<K,1QN0\leqslant B < K,\, 1\leqslant Q\leqslant N, and no character will be required more than once.

Output Format

Output one integer: the length of the shortest substring that satisfies all requirements. In particular, if no such substring exists, output "impossible".

5 2 2
0 1 1 0 1
0 1
1 1
2

13 4 3
1 1 3 2 0 1 2 0 0 0 0 3 1
0 2
2 1
1 2
7

5 3 1
1 2 0 1 2
0 2
impossible

Hint

Explanation for Sample 1

There are three substrings of length 22 that contain one occurrence each of characters 00 and 11: 0 1, 1 0, and 0 1. However, there is no substring of length 11 that satisfies the requirements, so the shortest valid substring has length 22.

Explanation for Sample 2

The shortest substring that satisfies the requirements is 1 3 2 0 1 2 0.

Explanation for Sample 3

In this string, the number of 0 is not enough.

Subtask Score Constraints
11 1616 1N100,R101\leqslant N\leqslant 100,\, R\leqslant 10
22 2424 1N4000,R101\leqslant N\leqslant 4\, 000,\, R\leqslant 10
33 2828 1N200000,R101\leqslant N\leqslant 200\, 000,\, R\leqslant 10
44 3232 1N2000001\leqslant N\leqslant 200\, 000

Thanks to Hatsune_Miku for providing the translation.

Translated by ChatGPT 5