题意:度度熊有一个N个数的数组,他想将数组从小到大 排好序,但是萌萌的度度熊只会下面这个操作:
任取数组中的一个数然后将它放置在数组的最后一个位置。
问最少操作多少次可以使得数组从小到大有序?
注意:题目中没有说明每个数是否唯一,我把它当做唯一的AC了。说明n个数中没有重复的!
思路:以前在hihoCoder做过类似的题。设
pos(i)
表示数字
i
当前所在的位置,假设我们将数组
AC代码
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 1000+5;
const int pour = 1000;
int a[50+5], pos[maxn<<1];
int main() {
int n;
while(scanf("%d", &n) == 1) {
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i] += pour;
pos[a[i]] = i;
}
sort(a, a+n);
int now = n, tol = 0;
for(int i = 1; i < n; i++) {
int l = a[i-1];
if(pos[l] > pos[a[i]]) {
pos[a[i]] = now++;
tol++;
}
}
printf("%d\n", tol);
}
return 0;
}
如有不当之处欢迎指出!