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

排个序,然后和排序前对照,不一样就往后找到应该在这一位上的数,然后交换

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int a[3030];
int b[3030]; struct node
{
int x, y;
}pp[3030]; int main()
{
int n;
while (~scanf("%d", &n))
{
int cnt = 0;
for (int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
int x, y;
for (int i = 0; i < n; ++i)
{
if (a[i] == b[i])
{
continue;
}
x = i;
for (int j = i + 1; j < n; ++j)
{
if (a[j] == b[i])
{
y = j;
break;
}
}
++cnt;
pp[cnt].x = x;
pp[cnt].y = y;
a[x] ^= a[y];
a[y] ^= a[x];
a[x] ^= a[y];
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; ++i)
{
printf("%d %d\n", pp[i].x, pp[i].y);
}
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

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

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

    http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...

  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. Ubuntu下使用虚拟机安装Windows XP(sunvirtualbox)

    看完这个教程之后,网银?淘宝?CS?魔兽?---一个都不能少!好了,教程开始! 首先下载虚拟机,下载地址:http://download.virtualbox.org/virtualbox/3.1.0 ...

  2. Java实现字符全阵列阵列

    import org.junit.Test; public class AllSort { public void permutation(char[] buf, int start, int end ...

  3. adodb.RecordSet的属性和方法

    为了更精确地跟踪数据,要用RecordSet组件创建包括数据的游标,游标就是储存在内存中的数据: rs = Server.CreateObject("ADODB.RecordSet" ...

  4. shell split分析日志文件

    在处理的文件comment有如下格式: ............................ xxx.xxx.10.59 db1059 mynbdpdatadb.idc4. dbname R620 ...

  5. 给EasyUI的DateBox控件添加清除button

     EasyUI中间DateBox控制甚至没有被清除button.例如下面的附图: 真是不可思议,对于要求日期格式必须选择的情况下,不能清空日期,很不方便.      尽管能够通过手工改动EasyU ...

  6. effective c++ 条款26 postpone variable definition as long as possible

    因为构造和析构函数有开销,所以也许前面定义了,还没用函数就退出了. 所以比较好的方法是用到了才定义.

  7. 最简单的视频编码器:基于libx265(编码YUV为H.265)

    ===================================================== 最简单的视频编码器系列文章列表: 最简单的视频编码器:编译 最简单的视频编码器:基于libx ...

  8. mongodb 学习笔记05 --用户管理

    csdn的markdown编辑器真有够烂的,这篇文章又给弄丢了 启用认证 mongod 启动默认没有开启权限,你须要指定 –auth 启动.或者在配置文件里设置security.authorizati ...

  9. iOS的图表显示的实现

    在app通常有家居展览的照片,显示广告.或者头条新闻.通常网易新闻client 如图,红框框的位置就是一个典型的图展, 熟悉iOS的人肯定知道,这个是个UIScrollview,里面加几张图片就可以实 ...

  10. 游戏 TRAP(SNRS)AlphaBeta版本

    大家好,我是PuzzledBoy,大一(大二快).我是一个独立的游戏开发商,我的梦想是成为一名伟大的艺术家的第九 今天来公布我的第一个独立游戏TRAP(SNRS)的Alpha測试版啦啦啦~~~! 游戏 ...