CF 275.5 A. SwapSort

来源:互联网 发布:淘宝宝贝描述上传图片 编辑:程序博客网 时间:2024/06/05 03:59
http://codeforces.com/contest/489/problem/A
A. SwapSort

In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.

Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.

Input

The first line of the input contains integer n (1?≤?n?≤?3000) — the number of array elements. The second line contains elements of array: a0,?a1,?...,?an?-?1 (?-?109?≤?ai?≤?109), where ai is the i-th element of the array. The elements are numerated from 0 to n?-?1 from left to right. Some integers may appear in the array more than once.

Output

In the first line print k (0?≤?k?≤?n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers ij (0?≤?i,?j?≤?n?-?1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i?=?jand swap the same pair of elements multiple times.

If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.

Sample test(s)
input
5
5 2 5 1 4
output
2
0 3
4 2
input
6
10 20 20 40 60 60
output
0
input
2
101 100
output
1
0 1

题目大意就是给n个元素的数列,然后交换几次(<=n次)后,使数列递增。输出交换次数和按由小到大的顺序输出交换的下标。
解题思路:遍历当前数列,对于当前元素,在此元素后面找比他小的最小元素交换,并输出序号。。贪心就可以了。。感觉和选择排序挺像,其实当时想用冒泡排序了。。但感觉会超时。。后来还是写渣了

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;
int a[3005];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int k,m;
printf("%d\n",n);
for(int i=0;i<n;i++)
{
k=a[i];
m=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<k)
{
k=a[j];
m=j;
}
}
a[m]=a[i];///交换
a[i]=k;
printf("%d %d\n",i,m);
}
return 0;
}



0 0
原创粉丝点击