Finding Keys
Time limit2sMemory limit2048 MB
For each key on a circular keychain, find the smallest k whose k-pattern of up and down comparisons with the next k keys no other key shares.
- Level
Medium7 of 10
- Topics
- String, String matching, Sorting
- Solved
- No attempts yet
Problem
Wolfgang Amadeus Mozart has too many keys! He has keys of distinct lengths on his circular keychain. Unfortunately, Wolfgang can only judge whether a key fits into a door by its relative size compared to the keys around it.
Let the -pattern of a key be the sequence of relative lengths of the keys that follow key in clockwise order. For example, if the keychain has keys of lengths 1, 5, 3, 4, 2 in clockwise order, then the 3-pattern of the key of length 3 is "<>>", since 3 < 4, 4 > 2, and 2 > 1. The last key of length 2 is followed by the first key of length 1.
For each key, find the smallest such that its -pattern is unique, meaning no other key has the same -pattern. If no such exists, output -1.
Input
The first line contains a single integer (), the number of keys. Each of the next lines contains one integer between 1 and , the length of a key, in clockwise order. All key lengths are distinct.
Output
Output lines, one integer per line. The th integer is the smallest such that the -pattern of the th key in the input is unique. If no such exists, output -1.