#C9964. Reverse Subarray Sorting

    ID: 54115 Type: Default 1000ms 256MiB

Reverse Subarray Sorting

Reverse Subarray Sorting

You are given an array of integers. Your task is to determine if the array can be sorted in non-decreasing order by reversing exactly one contiguous subarray. If the array is already sorted, then output the indices 1 and 1 (using 1-based indexing). Otherwise, if reversing a single subarray can make the whole array sorted, output "YES" followed by the starting and ending indices of that subarray. If it is not possible, output "NO".

For example, consider the following cases:

  1. For the array [1, 2, 3, 4, 5], the array is already sorted, so the output is "YES 1 1".
  2. For the array [1, 3, 2, 4], reversing the subarray from the second to the third element yields [1, 2, 3, 4], so the output is "YES 2 3".
  3. For the array [5, 4, 3, 2, 1], reversing the entire array sorts it, so the output is "YES 1 5".
  4. For the array [1, 2, 3, 6, 5, 4], reversing the subarray from the fourth to the sixth element results in a sorted array, so the output is "YES 4 6".
  5. For the array [1, 5, 3, 2, 4], no single subarray reverse operation can sort the array, so the output is "NO".

The indices in the answer always start at 1. Use the following logical approach: first, compare the given array with its sorted version. Identify the first and last positions where they differ; these positions represent the potential subarray reversal boundaries. Reverse that subarray and check if the resulting array matches the sorted version.

Mathematically, if (A) is the original array and (A') is its sorted form, find indices (l) and (r) such that (A[l]) (\neq) (A'[l]) and (A[r]) (\neq) (A'[r]). If reversing (A[l\ldots r]) makes (A) equal to (A'), then output (YES) along with (l+1) and (r+1) (due to 1-based indexing); otherwise, output (NO).

inputFormat

The input is given via standard input (stdin). The first line contains an integer (T) denoting the number of test cases. Each test case consists of two lines. The first line of each test case contains an integer (n) representing the size of the array. The second line contains (n) space-separated integers representing the array elements.

outputFormat

For each test case, print one line as the output. If it is possible to sort the array by reversing a single subarray, output "YES l r", where (l) and (r) are the 1-based starting and ending indices of the subarray to be reversed. If not, print "NO".## sample

1
5
1 2 3 4 5
YES 1 1

</p>