C. Make It Good
You are given an array consisting of integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from to make it a good array. Recall that the prefix of the array is a subarray consisting several first elements: the prefix of the array of length is the array ().
The array of length is called good, if you can obtain a non-decreasing array () from it, repeating the following operation times (initially, is empty):
- select either the first or the last element of 𝑏, remove it from , and append it to the end of the array .
For example, if we do operations: take , then , then and at last , then becomes [b3,b4,…,bm−3]and 𝑐=[𝑏1,𝑏𝑚,𝑏𝑚−1,𝑏2].
Consider the following example: b=[1,2,3,4,4,2,1]. This array is good because we can obtain non-decreasing array from it by the following sequence of operations:
- take the first element of , so 𝑏=[2,3,4,4,2,1], 𝑐=[1];
- take the last element of , so 𝑏=[2,3,4,4,2], 𝑐=[1,1];
- take the last element of , so 𝑏=[2,3,4,4], 𝑐=[1,1,2];
- take the first element of , so 𝑏=[3,4,4], 𝑐=[1,1,2,2];
- take the first element of , so 𝑏=[4,4], 𝑐=[1,1,2,2,3];
- take the last element of , so 𝑏=[4], 𝑐=[1,1,2,2,3,4];
- take the only element of , so , — is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of to delete (erase), to make to be a good array. Note that the required length can be .
You have to answer independent test cases.
The first line of the input contains one integer () — the number of test cases. Then test cases follow.
The first line of the test case contains one integer 𝑛 (1≤𝑛≤2⋅105) — the length of 𝑎. The second line of the test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤2⋅1051≤ai≤2⋅105), where 𝑎𝑖ai is the 𝑖-th element of 𝑎.It is guaranteed that the sum of does not exceed ().
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from to make it a good array.
5 4 1 2 3 4 7 4 3 3 8 4 5 2 3 1 1 1 7 1 3 1 4 5 3 2 5 5 4 3 2 3
0 4 0 2 3
In the first test case of the example, the array is already good, so we don't need to erase any prefix.In the second test case of the example, the initial array is not good. Let's erase first elements of , the result is . The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.
Comments
Post a Comment
Please give us your valuable feedback