CF 275.5 A. SwapSort
来源:互联网 发布:淘宝宝贝描述上传图片 编辑:程序博客网 时间:2024/06/05 03:59
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
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
int a[3005];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int k,m;
printf("%d\n",n);
for(int i=0;i<n;i++)
{
k=a[i];
m=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<k)
{
k=a[j];
m=j;
}
}
a[m]=a[i];///交换
a[i]=k;
printf("%d %d\n",i,m);
}
return 0;
}
- CF 275.5 A. SwapSort
- A. SwapSort
- Codeforces 489A SwapSort 题解
- Codeforces Round #277.5 (Div. 2)A. SwapSort
- Codeforces Round #277.5 (Div. 2)A——SwapSort
- Codeforces #277.5 (Div. 2) A. SwapSort(基础:选择排序)
- Codeforces Round #277.5 (Div. 2)---A. SwapSort (sort)
- CF 2A Winner
- CF 128A Statues
- <cf>A. Exams
- CF 109A
- CF 111A
- CF 4A Watermelon
- CF 237A (Cash)
- CF 6 A - Triangle
- CF:96A.Football
- CF:208A. Dubstep
- CF:41A. Translation
- HDU-5053 the Sum of Cube
- HDU 2041
- LeetCode145 Binary Tree Postorder Traversal(迭代实现) Java
- POJ 1195 Mobile phones(二维树状数组)
- ZOJ1091 Knight Moves
- CF 275.5 A. SwapSort
- CF 277.5 B. BerSU Ball
- HDU 2594
- HDU1686Oulipo
- HDU3336Count the string
- HDU 4707Pet
- CF 253A. Anton and Letters
- CF 253B. Kolya and Tandem Repeat
- cust 学校选拔赛