Description
Select the leftmost Number of the sequence, and insert it to any position in the sequence. The number of times that the above operation must be repeated at least to change the sequence to an incremental sequence.
The first line of input contains an integer.
T(
T> 0), indicating a total
TGroup test data. For each group of test data, the first row contains an integer.
N(1 ≤
N≤ 105), indicating a total
NInteger. The second line contains
NDifferent integers (all of which are in the range of [1,109]) are described in sequence.
Output
For each group of test data, an integer is output, indicating at least how many times the above operations need to be repeated before this sequence can be changed to an incremental sequence.
Sample Input
331 2 331 3 251 5 4 3 2
Sample output
024
Hint
Source
The eighth session of the Program Design Competition for Central South University Students
// Set m to the length of the continuous ascending sequence ending with the last number. The answer is n-M.
For example, M: If it is 1 5 2 3 4, it is 3. 3, 2, and 1 are 1.
Because you can only move the number on the leftmost side, the optimal strategy is to keep the rising column on the rightmost side of what I am talking about and just move the remaining number.
#include<cstdio>inta[100005];intmain(){ intt,n,i,j,ans; scanf("%d",&t); while(t--) { ans=1; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(j=n-2;j>=0;j--) { if(a[j]<a[j+1]) ans++; else break; } printf("%d\n",n-ans); } return0;}
CSU-1401: insert sort