Codeforces Round #277.5 (Div. 2)---A. SwapSort (sort),

來源:互聯網
上載者:User

Codeforces Round #277.5 (Div. 2)---A. SwapSort (sort),

SwapSort
time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

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 i, j (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 = j and 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
55 2 5 1 4
output
20 34 2
input
610 20 20 40 60 60
output
0
input
2101 100
output
10 1




題意:給一組數,問怎麼可以實現用最少次的交換,實現升序排序,輸出交換過程。


解題思路:當時比賽竟然腦殘了,WA了兩發,貌似和逆序數很像,對於每個元素,都找他後面的最大值,若最大值和當前i位置的值不等,即交換,儲存一下交換記錄,最後輸出即可。






AC代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[3010];int b[120];int vis[120];typedef pair<int,int> P;P m[3010];int main(){    int n;    cin>>n;    for(int i = 0; i < n; i++)        cin>>a[i];    int sum = 0,ans = 0;    int i,j;    for(i = 0; i < n; i++)    {        int x = a[i];        int y = i;        for( j = i+1; j < n; j++)        {           if(x > a[j])           {                x = a[j];                y = j;           }        }        if(y != i)        {            m[sum].first = i;            m[sum++].second = y;            swap(a[i],a[y]);            ans++;        }    }    cout<<ans<<endl;    for(int i = 0; i < ans; i++)    {        cout<<m[i].first<<" "<<m[i].second<<endl;    }    return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.