Codeforces Round #277.5 (Div. 2)-A. SwapSort
http://codeforces.com/problemset/problem/489/A
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 = 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.
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
解题思路: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的更多相关文章
- 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 ...
- 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 ...
随机推荐
- JAVA基础--JAVA API集合框架(ArrayList、HashSet、HashMap使用)14
一.集合Collection 1. 集合介绍 变量:表示的内存中的一个空间,只能保存确定类型的单个数据 数组:表示的是内存中的多个连续的空间,这些空间中可以存储多个同类型的数据. 后期继续学习面向对象 ...
- [Xcode 实际操作]九、实用进阶-(15)屏幕截屏:截取当前屏幕上的显示内容
目录:[Swift]Xcode实际操作 本文将演示如何截取屏幕画面,并将截取图片,存入系统相册. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UI ...
- Java 序列化(转)
转自 http://www.infoq.com/cn/articles/cf-java-object-serialization-rmi 对于一个存在于Java虚拟机中的对象来说,其内部的状态只保持在 ...
- VS2015 : error LNK1168
VC在重新生成Debug目录下的exe文件时,需要先删除原先的exe文件.但因为文件正在运行或是被锁定等原因,删除不了,于是出现 LNK1168错误.可以到任务管理器先将exe文件关闭,一个简单粗暴的 ...
- CentOS7 adb
https://blog.csdn.net/u012700515/article/details/79021320
- ZROI 普及组模拟赛02总结
ZROI 普及组模拟赛02总结 先放[网址][http://zhengruioi.com/contest/96] 可能是有一段时间没有打这种正式的比赛了,今天打的很奇怪... T1 模拟水题 既然是普 ...
- websocket~~原理详细解说优秀文(转)
新项目有个实时刷新新消息的功能,具体的有实时更新别人的回复,实时推送验证请求等功能, 在网上发现了关于websocket的优秀文,地址: https://marvelapp.com/1759hgb6/ ...
- 接口测试02 - 无法绕过的json解析
概述: 先瞧一下什么是json.JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式. 它基于ECMAScript(w3c定制的js规范)的一个子集 ...
- [译]Understanding ECMAScript6 对象
对象 ECMAScript6将大量精力聚焦在提升对象的实用性性上.聚焦的意义在于JavaScript中几乎每一个值是由对象中的某种类型表示.此外,在一个普通的JavaScript程序中使用对象的数量持 ...
- Unix高级环境编程之fcntl函数
#include <fcntl.h> int fcntl(int fd, int cmd, ...) fcntl功能 复制一个现有的描述符 (cmd = F_DUPFD) ##### 返回 ...