Jumping on Stones
InterviewTime limit2sMemory limit1024 MB
Find the fewest 1 or 2 meter jumps from point 0 to point n that land only on stones, and print the lexicographically smallest jump sequence.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array, String
- Solved
- No attempts yet
Problem
Judy Hopps is running to a very important meeting with Nick, and suddenly a huge lake blocks her way. She has no time to go around it, so she needs another plan. Luckily, a few stones poke out of the lake's surface, and she can jump on them.
Treat the lake as a coordinate line. Judy stands at point 0, and the far end of the lake is at point . Judy is a rabbit and good at jumping, but she can only jump 1 or 2 meters forward (that is, from point she can reach or ). Given the coordinates of the stones, find the minimum number of jumps needed to get from point 0 to point . Judy also wants to do this as optimally as possible, so among all paths with the minimum number of jumps, she wants the lexicographically smallest one. Each jump is written as a number 1 or 2, where the -th number is the length of the -th jump.
Help Judy solve this quickly. Even though she is a lady, being very late to the meeting is rude.
Input
The first line contains two integers and (; ), the coordinate Judy needs to reach and the number of stones in the lake. The second line contains integers separated by spaces (), the coordinates of the stones. It is guaranteed that , and for every , .
Output
The first line contains the minimum number of jumps needed to get from coordinate 0 to coordinate . The second line contains the optimal path, a sequence of 1s and 2s without spaces. The path must have minimum length, and among all such paths it must be lexicographically smallest. If Judy cannot reach point without going around the lake, print -1 on a single line.
Hint
A sequence is lexicographically smaller than a sequence if and there is some such that and .