http://codeforces.com/problemset/problem/489/A

A. SwapSort
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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 ij (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.

Sample test(s)
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个数字,通过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的更多相关文章

  1. 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 ...

  2. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  3. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 535. Encode and Decode TinyURL(rand and srand)

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  2. CodeForces 689B【最短路】

    题意: 给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1. 思路: 直接建边,跑spfa就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...

  3. 2014-5-24 NOIP模拟赛

    Problem 1 护花(flower.cpp/c/pas) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时 ...

  4. spark sql 对接 HDFS

    上一篇博客我向大家介绍了如何快速地搭建spark run on standalone,下面我将介绍saprk sql 如何对接 hdfs 我们知道,在spark shell 中操作hdfs 上的数据是 ...

  5. one jar(转)

    http://blog.csdn.net/kabini/article/details/1598827

  6. 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构

    1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...

  7. Vue文件 引入.js文件 的组件

    Vue.component('remote-script', { render: function (createElement) { var self = this; return createEl ...

  8. window git bash客户端vimrc设置tab缩进

    从开发机写的代码,弄到windows上的客户端git提交,总是显示格式对不起的问题,问题是再vimrc上tab键的缩进不等于4个空格,然后就需要设置成和linux一样的四格缩进. 安装上git bas ...

  9. [poj 2104] K-th Number【主席树】

    传送门:http://poj.org/problem?id=2104 保存模版. #include <cstdio> #include <algorithm> #include ...

  10. Expert Python programming - Reading Notes

    1. MRO: method resolution order lookup order: L(MyClass) = [MyClass, merged(L(Base1), L(Base2), Base ...