题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1739
【思路分析】本题需要在n次交换内使得序列有序,学过数据结构的我们都知道在选择排序这种排序方法中,最多经过n-1次交换序列就会变为有序的序列,所以就用选择排序做一下了,。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
#define LL long long
int a[10005];
int b[10005],c[10005];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
int sum=0;
for(int i=1; i<=n-1; i++)
{
int minn=a[i],pos=i;
for(int j=i+1; j<=n; j++)
{
if(minn>a[j])
{
minn=a[j];
pos=j;
}
}
if(i!=pos)
{
int temp=a[i];
a[i]=a[pos];
a[pos]=temp;
b[sum]=i;
c[sum++]=pos;
}
}
printf("%d\n",sum);
for(int i=0; i<sum; i++)
{
printf("%d %d\n",b[i],c[i]);
}
}
return 0;
}