CF489A.SwapSort

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

本题中,你的目标是至多通过 nn 次交换将一个包含 nn 个整数的数组排序为非递减顺序。对于给定的数组,请找出一串交换操作序列,使其经依次执行后变为非递减有序数组。

注意:本题不要求最小化交换次数——你只需找出任意一个长度不超过 nn 的交换序列即可。

输入格式

The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: _a_0, _a_1, ..., a__n - 1 ( - 109 ≤ a__i ≤ 109), where a__i 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.

输入的第一行包含一个整数 nn(1≤n≤30001 \leq n \leq 3000)—— 表示数组元素的个数。
第二行包含数组的元素:a0, a1, ..., an−1a_0,\,a_1,\,...,\,a_{n-1}(−109≤ai≤109-10^9 \leq a_i \leq 10^9),其中 aia_i 是数组的第 ii 个元素。元素从左到右编号为 00 到 n−1n-1。某些整数可能在数组中出现多次。

输出格式

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 a__i and a__j. 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.

第一行输出 k(0 ≤ k ≤ n),表示交换操作的次数。接下来的 k 行每行描述一次交换操作。每次交换操作应以一对整数 i, j(0 ≤ i, j ≤ n − 1)的形式输出,表示交换数组元素 a__i 与 a__j。在每对索引中,i 与 j 的顺序可以任意。交换操作按输出中的顺序依次执行,即从第一行到最后一行。允许输出 i = j,也允许多次交换同一对元素。

若存在多种可行答案,输出任意一种即可。题目保证至少存在一个合法答案。

输入输出样例

  • 输入#1

    5
    5 2 5 1 4

    输出#1

    2
    0 3
    4 2
  • 输入#2

    6
    10 20 20 40 60 60

    输出#2

    0
  • 输入#3

    2
    101 100

    输出#3

    1
    0 1

输入解题思路,AI测评打分。不知道怎么写?

首页