#P1638. Minimum Ticket Cost for Complete Painter Exhibition
Minimum Ticket Cost for Complete Painter Exhibition
Minimum Ticket Cost for Complete Painter Exhibition
The museum is exhibiting paintings by the world’s best m painters. The paintings are arranged in a row and each painting is created by one of the painters, identified by an integer between 1 and m. When purchasing a ticket, a visitor must specify two numbers, a and b, which denote that he will view all the paintings from the a-th to the b-th (both inclusive). The cost of the ticket is exactly the number of paintings in the selected segment, i.e. the cost is b - a + 1.
Sept wishes to view the works of all the renowned painters upon entry. In order to minimize the ticket price, he wants to choose a and b such that the segment [a, b] contains at least one painting by each painter and the cost (i.e. b - a + 1) is minimized. It is guaranteed that there exists at least one valid segment. If there are multiple solutions, output the one with the smallest a.
Note: Any formulas in the description are written in \( \LaTeX \) format (for example, the cost is given by \( b - a + 1 \)).
inputFormat
The first line contains two integers n and m (\(1 \leq m \leq n\)). Here, n is the total number of paintings, and m is the number of painters. It is guaranteed that every painter (from 1 to \(m\)) appears at least once among the paintings.
The second line contains n integers. The i-th integer represents the painter's ID of the i-th painting.
outputFormat
Output two integers a and b, representing the starting and ending indices (1-indexed) of the optimal subarray such that all painters are included and the cost \( b - a + 1 \) is minimized. If multiple answers exist, print the one with the smallest a.
sample
8 3
2 1 3 2 1 2 3 1
1 3
</p>