luogu#P4792. [BalticOI 2018] 火星人的 DNA
[BalticOI 2018] 火星人的 DNA
Problem Description
This problem is translated from BalticOI 2018 Day 1 “Martian DNA”.
You are given a string of length over an alphabet of size , and requirements. Each requirement is: in the substring, character must appear at least times. Find the length of the shortest substring that satisfies all requirements.
Input Format
The first line contains three integers , representing the length of the string, the size of the alphabet, and the number of requirements. It is guaranteed that .
The second line contains space-separated integers describing the string. Characters are numbered starting from , and every character in the alphabet appears at least once.
The next lines each contain two integers and , describing one requirement. It is guaranteed that , 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 that contain one occurrence each of characters and : 0 1, 1 0, and 0 1. However, there is no substring of length that satisfies the requirements, so the shortest valid substring has length .
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 |
|---|---|---|
Thanks to Hatsune_Miku for providing the translation.
Translated by ChatGPT 5