Problem Description
DreamGrid has just found an integer sequence a1,a2,...,an in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move it to the beginning of the sequence.
What's the minimum number of operations needed to make the sequence non-decreasing?
Input
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n(1<=n<=10^5), indicating the length of the sequence.
The second line contains n integers a1,a2,...,an(1<=ai<=10^9), indicating the given sequence.
It's guaranteed that the sum of of all test cases will not exceed 10^6.
Output
For each test case output one line containing one integer, indicating the answer.
Sample Input
2
4
1 3 2 4
5
2 3 3 5 5Sample Output
2
0Hint
For the first sample test case, move the 3rd element to the front (so the sequence become {2, 1, 3, 4}), then move the 2nd element to the front (so the sequence become {1, 2, 3, 4}). Now the sequence is non-decreasing.
For the second sample test case, as the sequence is already sorted, no operation is needed.
题意:t 组数据,每组给出 n 个数,每次从 n 个数中选出 1 个数移动到序列的开头,问使序列不减少所需的最小操作次数
思路:建一个备份数组,将序列复制并排序后,从后向前与原数组比较,每找到一个相同的说明不需要移动,最后输出最小移动数即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int a[N];
int b[N];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=n; i++)
b[i]=a[i];
sort(b+1,b+n+1);
int cnt=n;
for(int i=cnt; i>=1; i--)
if(a[i]==b[cnt])
cnt=cnt-1;
printf("%d\n",cnt);
}
return 0;
}