http://codeforces.com/problemset/problem/489/A

A. SwapSort
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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 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个数字,通过k次交换使得他们升序。贪心,每次找到没有找到的最小的数字的下标与当前下标的数字交换即可

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <time.h>
 6 #include <math.h>
 7 
 8 using namespace std;
 9 
 const int MAXN = ;
 int n, num[MAXN], from[MAXN], to[MAXN];
 
 void solve(){
     memset(from, , sizeof(from));
     memset(to, , sizeof(to));
     int i, j, k = , minnum, t;
     for(i = ; i < n - ; i++){
         minnum = i;
         for(j = i + ; j < n; j++){
             if(num[j] < num[minnum]){
                 minnum = j;
             }
         }
         if(i == minnum){
             continue;
         }
         from[k] = i;
         to[k] = minnum;
         t = num[i], num[i] = num[minnum], num[minnum] = t;
         k++;
     }
     printf("%d\n", k);
     for(i = ; i < k; i++){
         printf("%d %d\n", from[i], to[i]);
     }
 }
 
 int main(){
     int i;
     while(scanf("%d", &n) != EOF){
         for(i = ; i < n; i++){
             scanf("%d", &num[i]);
         }
         solve();
     }
     return ;

47 }

Codeforces Round #277.5 (Div. 2)-A. SwapSort的更多相关文章

  1. Codeforces Round #277.5 (Div. 2)A——SwapSort

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  2. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  3. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  4. Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)

    http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...

  5. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  6. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  7. Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being

    http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...

  8. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  9. Codeforces Round #277.5 (Div. 2)-B. BerSU Ball

    http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...

随机推荐

  1. Weekly Contest 111-------->944. Delete Columns to Make Sorted

    We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose an ...

  2. 手游性能优化之深入理解Texture Compression

    http://gad.qq.com/article/detail/7154875 一.引子 手游项目开发日常里,经常有美术同学搞不清Photoshop制图软件与Unity3D游戏引擎之间的图片asse ...

  3. ComDom在使用函数CompileAssemblyFromFile时无法找到文件的错误

    public virtual CompilerResults CompileAssemblyFromFile( CompilerParameters options, params string[] ...

  4. Node.js 关于module的一些认知

    module是一个对象,在Node环境中运行js脚本,module会自动添加,并且系统会将函数封装到另一个函数中 例如: var module = { id: '.', exports: {} }; ...

  5. B. Mancala (Codeforces Round #478 (Div. 2))

    #include <bits/stdc++.h> using namespace std; ; typedef long long ll; ll a[maxn]; ll b[maxn]; ...

  6. 51Nod 1873 初中的算术

    大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...

  7. Java - Class版本号和UnsupportedClassVersionError

    问题分析 Java是向下兼容的,每一个jdk版本都有对应的class版本号(major + minor version numbers):如果用低版本的jvm去加载高版本jdk编译的类,就会报错:ja ...

  8. 在Mac上安装Ubuntu14.04虚拟机

    1.在macOS High Sierras上安装VMware for mac 下载地址:VMware Fusion 8.5.1  https://pan.baidu.com/s/1skQ1OyL 2. ...

  9. 用apache commons-pool2建立thrift连接池

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.具体的介绍可以看Apache的官方网站:http://thrift.apache.org/ . ...

  10. JavaScript特点、优缺点及常用框架

    参考来源:   http://www.cnblogs.com/SanMaoSpace/archive/2013/06/14/3136774.html