Sort Problem | ||||||
| ||||||
Description | ||||||
You have a sequence of N (1 <= N <= 10 000) numbers (none of the numbers is equal to others), can you let the sequence become ascending within N movements. | ||||||
Input | ||||||
There are multiple test cases. The first line is a positive integer T, indicating the number of test cases. For each test case: Line 1. A positive integer N. Line 2. This line contains N integers: a1, a2, ..., ai, ...,an (0 < ai <= 2 000 000 000) separated by space.
| ||||||
Output | ||||||
For each test case, output m at the first line. m is the times of operation. Then m lines follows. Each line consists two integer x, y meaning that you will change the number on position x and the number on position y.
| ||||||
Sample Input | ||||||
3 3 2 3 1 5 1 2 5 3 4 10 11 3 17 19 7 2 20 15 10 13 | ||||||
Sample Output | ||||||
2 1 3 2 3 2 3 4 4 5 7 1 6 3 5 4 9 5 6 6 10 7 8 8 10 | ||||||
Source | ||||||
哈理工2013春季校赛 - 现场赛 |
思路:用b数组复制输入的数组,然后对它排序,然后得到的序列,就是目标序列,我们把应该放在这个位子的数字放回到这个位子,就可以了~
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[10005];
int b[10005];
int ans[10005][2];
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]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int cont=0;
for(int i=1;i<=n;i++)
{
if(b[i]!=a[i])
{
for(int j=i+1;j<=n;j++)
{
if(a[j]==b[i])
{
int tmp=a[j];
a[j]=a[i];
a[i]=tmp;
ans[cont][0]=i;
ans[cont][1]=j;
cont++;
break;
}
}
}
}
printf("%d\n",cont);
for(int i=0;i<cont;i++)
{
printf("%d %d\n",ans[i][0],ans[i][1]);
}
}
}