"Codeforces 489A" Swapsort

Source: Internet
Author: User

Problem

Description

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

Note that in this problem you does not have a to minimize the number of Swaps-your task are to find any sequence that is no l Onger 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 ( - 9≤ ai ≤109), where ai am the i -th element of the array. The elements is numerated from 0 to N -1from left to right. Some integers may appear in the array more than once.

Output

In the first line printk(0≤ k n )-the number of swaps. Next  K  lines must contain the descriptions of the  K  swaps, one per line. Each swap should is printed as a pair of Integers  i ,  < Em>j   (0≤ i , j n -1), representing the swap of Elements&nbs P a i  and  A J . You can print indices in the pairs in any order. The swaps is performed in the "order they appear in the" output, from the first to the last. It is allowed to Print  i = J and swap the same pair of elements multiple time S.

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

Sample Input

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

Test instructions: Using less than n times of swap to arrange the order.

Analysis: Select sort, choose the smallest one after unsorted and then the first interchange that is not sorted, so that a maximum of n-1 can be sorted and the two-digit position of the interchange is recorded in an array.

#include <stdio.h>int n,i,j,a[3002],coun,temp,swap1[3001],swap2[3001];int Main () {    scanf ("%d", &n);    for (i=0; i<n; i++)        scanf ("%d", a+i);    for (i=0; i<n-1; i++)    {        int minj=i;        for (j=i+1; j<n; j + +)            if (A[j]<a[minj]) minj=j;        if (I!=minj)        {            swap1[coun]=i;            Swap2[coun]=minj;            Temp=a[minj];            A[minj]=a[i];            A[i]=temp;            coun++;        }    }    printf ("%d\n", Coun);    if (Coun)        for (i=0; i<coun; i++)            printf ("%d%d\n", Swap1[i],swap2[i]);    return 0;}

"Codeforces 489A" Swapsort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.