Hills And Valleys
Time limit2sMemory limit512 MB
Given a sequence of digits, find the interval to reverse so that the longest non-decreasing subsequence is as long as possible, and report that length with the interval.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String, Prefix sum
- Solved
- No attempts yet
Problem
Tauren has an integer sequence of length (1-based). He wants you to invert an interval () of , that is, replace with , so that the length of the longest non-decreasing subsequence of is as large as possible. Find that maximal length and any interval whose inversion achieves it.
A non-decreasing subsequence of with length is with and .
Input
The first line contains one integer , the number of test cases. Each test case is given as follows. The first line contains one integer . The second line contains integers written without spaces.
Constraints: , , for . The sum of over all test cases does not exceed .
Output
For each test case, print three integers , , and separated by spaces on one line, where is the maximal length and is the interval to invert.
Hint
In the first example, 864852302 becomes 032584682 after inverting , and one of its longest non-decreasing subsequences is 03588. In the second example, 203258468 becomes 023258468 after inverting , and one of its longest non-decreasing subsequences is 023588.