SwapSort

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121332#problem/A

Description

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 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

题意:

给出一个数组,进行不超过n次交换操作后保持升序;

题解:

由于没有要求输出最少的次数;

直接跑一遍选择排序法就可以了,复杂度有点高,不过可以接受.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 3300
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int a[maxn];
int ans[maxn+100][2]; int main(int argc, char const *argv[])
{
//IN; int ca = 1;
while(scanf("%d", &n) != EOF)
{
int cnt = 0;
for(int i=1; i<=n; i++) scanf("%d", &a[i]);
//sort(a+1, a+1+n);
for(int i=1; i<=n; i++) {
int p=i, mimi = a[i];
for(int j=i+1; j<=n; j++) {
if(a[j] < mimi) mimi = a[p=j];
}
if(p==i) continue;
swap(a[i], a[p]);
//printf("%d %d\n", i-1,p-1);
ans[cnt][0]=i-1;
ans[cnt][1]=p-1;
cnt++;
} printf("%d\n", cnt);
for(int i=0; i<cnt; i++)
printf("%d %d\n", ans[i][0], ans[i][1]);
} return 0;
}

CodeForces 489A SwapSort (选择排序法)的更多相关文章

  1. C语言实现冒泡排序法和选择排序法代码参考

    为了易用,我编写排序函数,这和直接在主调函数中用是差不多的. 我认为选择排序法更好理解!请注意 i 和 j ,在写代码时别弄错了,不然很难找到错误! 冒泡排序法 void sort(int * ar, ...

  2. c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法

    本文详细叙述和实现了快速排序算法,冒泡排序 选择排序 插入排序比较简单,原理在这里不再详述,直接用代码进行了实现. 快速排序法(quicksort)是目前所公认最快的排序方法之一(视解题的对象而定), ...

  3. php 四种基础算法 ---- 选择排序法

    2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...

  4. 选择排序法-java详解案例

    /**  * 功能:选择排序法 * 思想:第一次从R[0]-R[N-1]中选取最小值,与R[0]交换,第二次从R[1]-R[N-1]中选取最小值,与R[1]交换, * 第三次从R[2]-R[N-1]中 ...

  5. Java 快速排序法 冒泡排序法 选择排序法 插入排序法

    1.快速排序的原理: 选择一个关键值作为基准值.比基准值小的都在左边序列(一般是无序的),比基准值大的都在右边(一般是无序的). 从后往前比较,用基准值和最后一个值比较,如果比基准值小的交换位置,如果 ...

  6. 基于python语言的经典排序法(冒泡法和选择排序法)

    前 每逢周末就遇雨期,闲暇之余,捣鼓了下python,心心念想学习,今天就在电脑上装了个2.7,学习了下经典算法,冒泡与选择排序法 第一次写关于python的文章,说的不当之处,多多指正,我积极改正 ...

  7. 选择排序法、冒泡排序法、插入排序法、系统提供的底层sort方法排序之毫秒级比较

    我的代码: package PlaneGame;/** * 选择排序法.冒泡排序法.插入排序法.系统提供的底层sort方法排序之毫秒级比较 * @author Administrator */impo ...

  8. c语言:简单排序:冒泡排序法、选择排序法、插入排序法(待写)

    1.冒泡排序法: 假设有n个数需要按从小到大排序,冒泡排序的原理是,在这一排数字中,将第一个数与第二个数比较大小,如果后面的比前面的小,就将他们交换位置.然后再比较第二个和第三个,再交换,直到第n-1 ...

  9. Java使用选择排序法对数组排序

    编写程序,实现将输入的字符串转换为一维数组,并使用选择排序法对数组进行排序. 思路如下: 点击"生成随机数"按钮,创建Random随机数对象: 使用JTextArea的setTex ...

随机推荐

  1. 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)

    一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...

  2. $.toJSON的用法或把数组转换成json类型

    1. html页面全部代码 <html> <head>     <title></title> <script src="../../S ...

  3. CSS在不同浏览器兼容问题,margin偏移/offset溢出等

    margin在垂直取值时取最大值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  4. 你用哪种工具进行iOS app自动化功能测试?

    原文见http://www.cocoachina.com/applenews/devnews/2013/1111/7332.html

  5. (任寒韬)WebApp群主 - MobileTech 资料

    web app : http://www.lightapp.cn/brand/index/4101 https://github.com/jtyjty99999/mobileTech/blob/mas ...

  6. How to delete a team project from Team Foundation Service (tfs.visualstudio.com)

    C:\project>tfsdeleteproject /collection:https://buckh-test2.visualstudio.com/DefaultCollection Te ...

  7. CCScrollView 实现帮助界面、关卡选择

    本文出自[无间落叶]:http://blog.leafsoar.com/archives/2013/07-27.html 本文介绍了 CCScrollView 来编写帮助界面和关卡选择界面的方法,在编 ...

  8. ubuntun安装ssh,并远程链接服务器操作

    SSH是一种以安全.加密方式连接远程主机或服务器的方法.SSH服务器接受从有SSH的客户机的连接,允许操作者象在本地一样地登录系统.你可以用SSH从远程运行shell和X程序. (1)安装SSH服务器 ...

  9. 【转】让 cocos2d-x 的 CCHttpRequest 支持https

    肖锐(Cooki)个人原创,欢迎转载,转载请注明地址,肖锐(Cooki)的技术博客 http://blog.csdn.net/xiao0026  由于游戏用到了网络头像, 今天发现换成facebook ...

  10. How to Download APK Files from Google Play Store

    Evozi, an Android app developer, offers a one-click online APK download app that lets you download t ...