度度熊有一个N个数的数组,他想将数组从小到大 排好序,但是萌萌的度度熊只会下面这个操作:
任取数组中的一个数然后将它放置在数组的最后一个位置。
问最少操作多少次可以使得数组从小到大有序?
输入描述:
首先输入一个正整数N,接下来的一行输入N个整数。(N <= 50, 每个数的绝对值小于等于1000)
输出描述:
输出一个整数表示最少的操作次数。
输入例子1:
4
19 7 8 25
输出例子1:
2
结题思路:
设输入的数组为a,对a进行排序后的数组为b;然后仅仅对a从头到尾遍历一遍,按照b中的数据,严格进行找对应的值,个数为cnt,答案就是n-cnt;因为除去这cnt个,其他的肯定是要调到最后面去的,然而这ans-cnt数里面较小的数先放到后面,总的也就是只要ans-cnt次
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>
#include <iostream>
#include <math.h>
#include <string>
#include <queue>
#include <stdlib.h>
#include <map>
#include <set>
# define f(i, m, n) for(int i=m; i<=n; i++)
# define sf(x) scanf("%d", &x)
# define sff(x) scanf("%lf", &x)
# define pfln(x) printf("%d\n", x);
# define pf(x) printf("%d", x);
# define mem(s) memset(s, 0, sizeof(s));
using namespace std;
int a[110], b[110];
int main(){
int n;
sf(n) ;
f(i, 0, n-1){
sf(a[i]);
b[i]=a[i];
}
sort(b, b+n);
int j=0;
int c=0;
f(i, 0, n-1){
if(a[i]==b[j]){
c++;
j++;
}
}
pfln(n-c);
return 0;
}