Rise and Fall
Time limit1sMemory limit1024 MB
Given a huge decimal string, find the largest number no greater than it whose digits split into a nondecreasing prefix and a nonincreasing suffix.
- Level
Hard8 of 10
- Topics
- Greedy, String, Implementation, Math
- Solved
- No attempts yet
Problem
A number is said to Rise and Fall if its decimal representation can be split into two parts (either of which may be empty) such that the first part has digits in nondecreasing order and the second part has digits in nonincreasing order.
Compute the largest number less than or equal to an input number that rises and falls.
Input
The first line of input contains an integer (), the number of test cases.
Each of the next lines contains a single integer (). Each line is one test case.
- Note: this is not a typo. The integer can be up to digits long.
The sum of the lengths of all input test cases does not exceed .
Output
For each test case, output a single line with one integer: the largest number less than or equal to the for that test case that rises and falls.