Tidy Numbers (Large)
Time limit5sMemory limit512 MB
Given N up to 10^18, find the largest number not exceeding N whose decimal digits are in non-decreasing order.
- Level
Medium5 of 10
- Topics
- Greedy, Math, Implementation, String
- Solved
- No attempts yet
Problem
Tatiana likes to keep things tidy. Her toys are sorted from smallest to largest, her pencils from shortest to longest, and her computers from oldest to newest. One day, while practicing her counting, she noticed that some integers have their digits in non-decreasing order when written in base 10 with no leading zeroes. Examples are 8, 123, 555, and 224488. She decided to call such an integer a tidy number. Numbers without this property, such as 20, 321, 495, and 999990, are not tidy.
She has just counted every positive integer from 1 to in ascending order. What was the last tidy number she counted?
Input
The first line contains the number of test cases . Each of the next lines describes one test case with a single integer , the last number Tatiana counted.
Limits
Output
For each test case, print one line of the form Case #x: y, where is the test case number starting from 1 and is the last tidy number Tatiana counted.