PDP-29 (2017) - Phase B' Junior High School (colors)

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem types
Allowed languages
C, C++, Java, Pascal, Python

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 1 to K.

pdp-2017-BG-fig-1.svg

In the example above, the ribbon has a length of N = 10 centimeters, and there are K = 3 colors: yellow (1), orange (2), and green (3). 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 1 to K. 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 \le Ν \le 1.000.000) and the number of colors K (1 \le Κ \le 100.000). The second line contains exactly N integers c (1 \le c \le 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 1 and K. If there is no such piece (because not all colors were present in the original ribbon), then the output should be the number 0 (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:

pdp-2017-BG-fig-2.svg


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 4 is missing from the ribbon.

Notes:
  • Maximum available memory: 64 ΜΒ
  • Maximum execution time: 1 sec

Comments

There are no comments at the moment.