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. 重温sql 设计的基本三大范式

    第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 例如:顾客表(姓名.编号.地址.……)其中"地址"列还可 ...

  2. 翻转链表中相邻的k个节点

    示例: 输入:1->2->3->4->5 k=2 输出:2->1->4->3->5 k=3输出:3->2->1->4->5 Py ...

  3. NGUI研究院之UISprite和UITexture浅谈

    NGUI的三大组件,UILabel.UISprite.UITexture,它们三个同时都继承UIWidget.先回到一个很郁闷的话题上,到底是优化DrawCall还是优化内存. UISprite : ...

  4. c# Marshal.PtrToStructure(StructPtr, typeof(T)); 特别注意

    以下异常:Marshal.PtrToStructure(StructPtr, typeof(T)); 原因:  在实际使用中 T 没有一个 parameterless  constructor 于是加 ...

  5. EasyUI/TopJUI可编辑表格的列根据返回数据判断是使用 combobox 还是 numberbox

    这两天研究了一下topjui的可编辑表格edatagrid,想在每一列的后面根据返回的数据判断是使用 combobox 还是 numberbox,期间遇到了一些坑,下面实现代码,需要的朋友可以参考一下 ...

  6. Tomcat日志文件的输出在Linux和Windows下的差异

    前言 最近老大发现Tomcat的日志文件catalina.out里存在着大量的和公司项目相关的log信息,因为一般都是会使用日志框架并另外将log信息输出到另外的文件里的,catalina.out文件 ...

  7. [软件工程基础]PhyLab 需求与功能分析改进文档

    NABCD 模型 Need 需求 根据 Default 的需求文档,物理实验网站对于北航大二学生完成物理实验有较大的帮助,反馈较好.由于在 2016-2017 春季学期,网站数据库因为不明原因被删除了 ...

  8. ubuntu dpkg命令总结

    dpkg是Debian系统的后台包管理器,类似RPM.也是Debian包管理系统的中流砥柱,负责安全卸载软件包,配置,以及维护已安装的软件包.由于ubuntu和Debian乃一脉相承,所以很多命令是不 ...

  9. 调试PHP

    echo "<br/>"; print_r($array);

  10. arcgis jsapi接口入门系列(10):图形高亮

    jsapi也有提供高亮的实现接口,但这里没用,而用的是一种改变图形(graphic)样式的思路 本文实现效果是:地图有多个面图形,当鼠标移动到面的上方,面高亮显示,鼠标移出后高亮解除 初始化 //高亮 ...