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 ...
随机推荐
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- improper Advertising identifier [IDFA] Usage. Your app contains the Advertising Identifier [IDFA] AP
找到答案了.随便传个包上去.然后拒绝掉,又一次prepare to upload.就会出现选项. 相应选择就好了.
- C++学习笔记9-运算符重载
1. 重载运营商必须有一个类类型的操作数 对于内置类型运营商.它的意义不能改变. 例如,内置整数加法运算不能被重新定义: // error: cannotredefine built-in opera ...
- 每天努力一点之SQL
今天工作当中遇到一个问题:统计信息并导出EXcel 报表. 刚开始只做了统计信息: 如下图 请看最后一列的数据. 我当时想都从数据库里取出来,但是由于我能力有限没有做出来.先贴下后来写的SQL 语句. ...
- GotoTest
循环标签跳出循环 public class GotoTest { public static void main(String[] args) { int[][] array={ {1,20,3}, ...
- cocos2D(五岁以下儿童)---- CCNode
本将主要介绍下CCNode这个类.CCNode是全部节点的基类,当中包含我们经常使用的CCScene(场景).CCLayer(图层).CCSprite(精灵)等.它是一个不可以可视化显示的抽象类,仅仅 ...
- 小说mvvm
与多样化和复杂的前,设计模式不再是后端专有名词.从最初的面向对象的,框架制定了到现在为止mvc等一下,今天,它主要是关于Model-View-ViewModel(MVVM). 对于mvc大家可能都会相 ...
- 有JSON中字段最好是【字符】而非【enum】想到
最近听了牛人一句: 1,如果协议中定义了tag的话,协议的解析就不会依赖到变化,那么开发的话也更为独立. eg: good: name=“zl”, gender=“f” bad: name=" ...
- Cocos2d-x3.0游戏实例《不要救我》第十篇(结束)——使用Json配置数据类型的怪物
如今我们有2种类型的怪物,并且创建的时候是写死在代码里的,这是要作死的节奏~ 所以.必须可配置.不然会累死人的. ; i < size; ++i) { int id = root[i][&quo ...
- wxWidgets+wxSmith版电子词典
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...