Codeforces Round #277.5 (Div. 2)A——SwapSort
1 second
256 megabytes
standard input
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.
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.
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.
5
5 2 5 1 4
2
0 3
4 2
6
10 20 20 40 60 60
0
2
101 100
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的更多相关文章
- 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 ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- 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 ...
- 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 ...
- Codeforces Round #277.5 (Div. 2)部分题解
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- jenkins配置.net mvc网站
jenkins配置.net mvc网站 上一篇使用jenkins配置.net mvc网站进行持续集成一只是简单介绍了jenkins构建站点到本地服务器,这一篇,就来讲解如何部署站点到指定的服务器上面. ...
- Django操作model时刻,一个错误:AttributeError:’ProgrammingError’ object has no attribute ‘__traceback__’
原因:在Django项目下对应的应用以下的models.py配置的model(也就是class)没有创建成对应的表. 这是怎么回事呢? 首先,将models.py里面的model创建成相应的数据库表的 ...
- 冒泡排序算法(Java)
冒泡排序即每次遍历.相邻数字间进行比較,前者大于后者进行交换,不断将最大值后移,直至沉至最后位置:算法关键要点在于确定每次循环的边界. 后面两种算法则是对冒泡排序一定程度上的改良,但相对于其它排 ...
- 玩转Web之Json(四)---json与(Object/List/Map)的相互转化
在做web应用时,经常需要将json转化成Object/list/map或者将Object/List/map转化成json,通过简单封装可以在写代码是减轻很多负担.本文将给出json转化的一系列方法. ...
- C#改动文件或目录的权限,为指定用户、用户组加入全然控制权限
C#改动文件或文件夹的权限,为指定用户.用户组加入全然控制权限 //给Excel文件加入"Everyone,Users"用户组的全然控制权限 FileInfo fi = new F ...
- hdu 5094 Maze(水搜索)
题目意思:说有一个人在(1,1) 他的目标点在(n,m) 每次是4方向的移动: 限制条件:有的各自之间有墙 或者门,强不可通过,有对应的要钥匙可以开启这个类型的所有门: 问题:求最少步骤数(和): 类 ...
- effective c++ 条款4 make sure that objects are initialized before they are used
1 c++ 类的数据成员的初始化发生在构造函数前 class InitialData { public: int data1; int data2; InitialData(int a, int b) ...
- 【Stackoverflow好问题】将InputStream转换为String
不重复造轮子. 最可靠的方法,或者使用Apache commons IOUtils 这样简单几行代码就搞定了 StringWriter writer = new StringWriter(); IO ...
- Haskell 几乎无疼痛入门指南
当他重装Linux 机会虚拟机,安装 haskell 录制的过程中有什么.的方式来帮助那些谁在徘徊haskell进入外读者. 基本概念: Haskell : 是一门通用函数式语言.差点儿能够进行不论什 ...
- Apple Watch 2.0 数据通讯
经常会碰到Watch app和WatchKit extension需要访问同一个文件.比如,使用一个自定义的字体,播放多媒体文件.有两种方法完成这个任务. 设计的时候,每个包放一份文件.它们分别访问自 ...