zoukankan      html  css  js  c++  java
  • 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 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 
    10 const int MAXN = 3010;
    11 int n, num[MAXN], from[MAXN], to[MAXN];
    12 
    13 void solve(){
    14     memset(from0sizeof(from));
    15     memset(to, 0sizeof(to));
    16     int i, j, k = 0, minnum, t;
    17     for(i = 0; i < n - 1; i++){
    18         minnum = i;
    19         for(j = i + 1; j < n; j++){
    20             if(num[j] < num[minnum]){
    21                 minnum = j;
    22             }
    23         }
    24         if(i == minnum){
    25             continue;
    26         }
    27         from[k] = i;
    28         to[k] = minnum;
    29         t = num[i], num[i] = num[minnum], num[minnum] = t;
    30         k++;
    31     }
    32     printf("%d ", k);
    33     for(i = 0; i < k; i++){
    34         printf("%d %d "from[i], to[i]);
    35     }
    36 }
    37 
    38 int main(){
    39     int i;
    40     while(scanf("%d", &n) != EOF){
    41         for(i = 0; i < n; i++){
    42             scanf("%d", &num[i]);
    43         }
    44         solve();
    45     }
    46     return 0;

    47 } 

  • 相关阅读:
    面向对象(OOP:Objdec Oriented Programming)
    vue中v-model和v-bind区别
    DateTimeFormat
    html中frameset简介
    学习及资料地址
    mybatis+oracle批量新增带序列List对象
    Io流读取并输出文件(例如.mp3格式文件)
    Java从服务器下载图片保存到本地
    转:Java DecimalFormat的主要功能及使用方法
    数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4113639.html
Copyright © 2011-2022 走看看