#C8702. Minimum Tree Sequence
Minimum Tree Sequence
Minimum Tree Sequence
You are given a list of integers representing different types of trees along a line. There are n types of trees which are labeled from 1 to n. Your task is to find the length of the smallest contiguous subsequence that contains all n distinct tree types. If such a subsequence does not exist, output INF
.
Formally, given an array (trees) of length (m) and an integer (n), find the minimum length (L) such that there exists an index (i) with (1 \leq i \leq m-L+1) for which the subarray (trees[i], trees[i+1], \dots, trees[i+L-1]) contains every integer from (1) to (n) at least once. If no such subsequence exists, output INF
.
inputFormat
The input is read from standard input (stdin) and consists of two lines. The first line contains two space-separated integers: (m) (the number of trees) and (n) (the total number of tree types). The second line contains (m) space-separated integers, representing the array (trees).
outputFormat
Output a single line to standard output (stdout) containing the length of the smallest contiguous subsequence that includes all tree types from 1 to (n). If no such subsequence exists, output INF
.## sample
12 5
1 2 1 3 2 1 5 1 3 2 4 5
5