CodeForces 489A SwapSort (选择排序法)
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 (选择排序法)的更多相关文章
- C语言实现冒泡排序法和选择排序法代码参考
为了易用,我编写排序函数,这和直接在主调函数中用是差不多的. 我认为选择排序法更好理解!请注意 i 和 j ,在写代码时别弄错了,不然很难找到错误! 冒泡排序法 void sort(int * ar, ...
- c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法
本文详细叙述和实现了快速排序算法,冒泡排序 选择排序 插入排序比较简单,原理在这里不再详述,直接用代码进行了实现. 快速排序法(quicksort)是目前所公认最快的排序方法之一(视解题的对象而定), ...
- php 四种基础算法 ---- 选择排序法
2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...
- 选择排序法-java详解案例
/** * 功能:选择排序法 * 思想:第一次从R[0]-R[N-1]中选取最小值,与R[0]交换,第二次从R[1]-R[N-1]中选取最小值,与R[1]交换, * 第三次从R[2]-R[N-1]中 ...
- Java 快速排序法 冒泡排序法 选择排序法 插入排序法
1.快速排序的原理: 选择一个关键值作为基准值.比基准值小的都在左边序列(一般是无序的),比基准值大的都在右边(一般是无序的). 从后往前比较,用基准值和最后一个值比较,如果比基准值小的交换位置,如果 ...
- 基于python语言的经典排序法(冒泡法和选择排序法)
前 每逢周末就遇雨期,闲暇之余,捣鼓了下python,心心念想学习,今天就在电脑上装了个2.7,学习了下经典算法,冒泡与选择排序法 第一次写关于python的文章,说的不当之处,多多指正,我积极改正 ...
- 选择排序法、冒泡排序法、插入排序法、系统提供的底层sort方法排序之毫秒级比较
我的代码: package PlaneGame;/** * 选择排序法.冒泡排序法.插入排序法.系统提供的底层sort方法排序之毫秒级比较 * @author Administrator */impo ...
- c语言:简单排序:冒泡排序法、选择排序法、插入排序法(待写)
1.冒泡排序法: 假设有n个数需要按从小到大排序,冒泡排序的原理是,在这一排数字中,将第一个数与第二个数比较大小,如果后面的比前面的小,就将他们交换位置.然后再比较第二个和第三个,再交换,直到第n-1 ...
- Java使用选择排序法对数组排序
编写程序,实现将输入的字符串转换为一维数组,并使用选择排序法对数组进行排序. 思路如下: 点击"生成随机数"按钮,创建Random随机数对象: 使用JTextArea的setTex ...
随机推荐
- EBS报表输出文件格式控制
具体使用方法:1.添加用户参数p_conc_request_id2.在BeforeReport trigger中添加srw.user_exit('FND SRWINIT'); 和Af ...
- Codeforces 383A - Milking cows
原题地址:http://codeforces.com/problemset/problem/383/A 题目大意:有 n 头奶牛,全部看着左边或者右边,现在开始给奶牛挤奶,给一头奶牛挤奶时,所有能看到 ...
- 函数fsp_try_extend_data_file
扩展表空间 /***********************************************************************//** Tries to extend t ...
- HDU 2433 Travel (最短路,BFS,变形)
题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...
- MYSQL自动备份策略的选择
目前流行几种备份方式: 1.逻辑备份:使用mysql自带的mysqldump工具进行备份.备份成sql文件形式.优点:最大好处是能够与正在运行的mysql自动协同工作,在运行期间可以确保备份是当时的点 ...
- noip2005提高组题解
05年的题目绝对是自2000年以来难度最大的.后三题的难度系数分别为0.2.0.2.0.3,而前面几年的题目中每年最多只出现一道难度系数为0.2的题目,其难度可见一斑. 强烈推荐这个 PPT,每道题都 ...
- erl0003-ets 几种类型的区别和ets效率建议 <转>
rlang内置大数据量数据库 ets,dets 初窥 发布日期:2011-10-24 18:45:48 作者:dp studio ets是Erlang term storage的缩写, dets则 ...
- poj 2762 Going from u to v or from v to u?
题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...
- Buffer cache 的调整与优化
Buffer cache 的调整与优化 -============================== -- Buffer cache 的调整与优化(一) --==================== ...
- android左右滑动加载分页以及动态加载数据
android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...