Anomaly Mahjong 9
InterviewTime limit2sMemory limit1024 MB
Given up to 100000 tiles from 34 kinds, find the shortest contiguous span that contains 5 tiles of the same kind, or -1 if no span does.
Problem
A mahjong tile (牌) comes in 34 kinds, with 4 copies of each kind, for 136 tiles in total. The kinds are as follows.
You are practicing by shuffling the tiles and laying them down one at a time. An anomaly then changed the tile kinds. Having learned that each kind has 4 copies, you resolved it.
A bigger anomaly is happening now. Besides the change in kinds, the number of tiles has grown, and up to tiles will be laid. You did not notice the odd count. So you must check whether any kind appears 5 or more times, as before. Your memory is finite, so you can only remember the most recently laid tiles. In other words, to notice the anomaly, right after laying some tile, at least 5 of the tiles in your memory must be the same kind.
Find the minimum memory needed to notice the anomaly. If no memory size lets you notice it, print -1.
Input
The first line contains , the number of tiles to lay.
The second line contains the tiles in the order you lay them, separated by spaces. Every tile is one of the 34 kinds above.
Output
Print the minimum memory needed to notice the anomaly. If you cannot notice it no matter how large your memory is, print -1.