COLORED RIBBON
My cat likes to play with a colorful ribbon. The ribbon has a length of N centimeters, and each centimeter is colored with one of K possible colors, numbered from to .
In the example above, the ribbon has a length of centimeters, and there are colors: yellow , orange , and green . As you can see, each centimeter of the ribbon is colored with one of these.
My cat has artistic tendencies and refuses to play with the ribbon unless it has all the colors from to . However, if the ribbon is too long, it easily gets tangled, and my cat gets annoyed.
Help me find and cut the smallest possible piece of the ribbon that contains all the colors!
Problem
Develop a program in one of the IOI Languages (PASCAL, C, C++, Java) that, after reading the length of the ribbon, the number of colors, and the color codes for each centimeter of the ribbon, returns the length of the smallest possible piece of the ribbon that contains all the colors.
Input Files:
The input files named colors.in are text files with the following structure: They have exactly two lines. The first line contains two integers separated by a single space: the length of the ribbon N (1 Ν 1.000.000) and the number of colors K (1 Κ 100.000). The second line contains exactly integers c (1 c K) separated in pairs by a single space. These numbers are the color codes with which each centimeter of the ribbon is colored, from one end to the other.
Output Files:
The output files named colors.out are text files with the following structure: They have exactly one line with exactly one integer: the length of the smallest possible piece of the ribbon that contains all the colors between and . If there is no such piece (because not all colors were present in the original ribbon), then the output should be the number (zero).
Examples of Input - Output Files:
1st
STDIN (colors.in)
10 3
1 3 1 3 1 3 3 2 2 1
STDOUT (colors.out)
4
This example corresponds to the ribbon in the diagram on the previous page. There are two segments that contain all the colors and have a length of 4:
2nd
STDIN (colors.in)
10 4
2 1 4 4 4 1 1 1 4 3
STDOUT (colors.out)
10
In this example, we need to keep the entire ribbon.
3d
STDIN (colors.in)
10 5
1 2 2 5 1 3 1 2 1 3
STDOUT (colors.out)
0
In this example, color is missing from the ribbon.
Notes:
- Maximum available memory: 64 ΜΒ
- Maximum execution time: 1 sec
Comments