Pandora's Box
Time limit2sMemory limit1024 MB
Given n mountain heights, find the fewest segment operations on equal-height endpoints that make the heights nondecreasing, and list the segments in order.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Hash map, Intervals
- Solved
- No attempts yet
Problem
To defeat Ares, the god of war, Kratos must reach Pandora's box, which can grant its owner truly divine power. Unfortunately for the Spartan, the box lies in the depths of the Temple of Pandora, and mountains stand on the way to the temple. The height of the -th mountain is meters.
The only thing the mighty Kratos fears is heights. That is why he never descends or jumps down, since large drops in height frighten him. He does jump very well, and he has a divine skill: if the height of the -th mountain equals the height of the -th, Kratos can make all mountains on the segment from to inclusive have height in a single action.
To reach the temple, Kratos must apply his skill to some segments so that he never has to go down, that is, the condition holds. Kratos is in a hurry and does not want to be noticed by Ares, so he cannot change the heights too often. Help Kratos reach the temple with the minimum number of actions.
Input
The first line contains an integer , the number of mountains on the way to the temple ().
The second line contains integers , the heights of the mountains ().
Output
In the first line, print , the minimum number of actions Kratos must perform to reach the temple.
In each of the next lines, print two numbers and , the bounds of the segment for the next leveling action. Print the actions in the order Kratos must perform them.
If there is no solution, print -1 on a single line.