有趣的排序

度度熊有一个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;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值