您的位置:首页 > 其它

CF#277.5 (Div. 2) A.(技巧题,类似动规)

2014-11-19 12:54 417 查看
A. SwapSort

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output
题目链接:http://codeforces.com/contest/489/problem/A

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 = 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,然后输入n个整数,求在n次交换之内把这个序列变为升序所需的次数,并且输出交换路径。                                        首先要搞清楚它要求在n次交换内完成,我因此贡献了WA,如果没有这个限制的话,可以暴力,但是这道题暴力的话好像会超时,因为我出了一发Time Limite Exceeded。  我们可以在O(n)的时间内遍历数组,每次对于当前的 i ,我们再从 i + 1开始遍历,找到 i 后面的最小值并记录下标,如果里层遍历后,i == k,那么扫描下一个 i ;否则交换a[i] 和a[k],并且记录路径。
	为什么这样做是不错的选择呢,可以这样想,你是按从小到大顺序找的,相当于你已知这个位置,往这个位置里填数,,每一次填完,保证在它之前已经是升序的,有种类似于dp的思想。



完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
int n;
const int maxn = 1000001;

int a[maxn]  , t1[maxn] , t2[maxn];

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    while(~scanf("%d",&n))
    {

        for(int i = 0 ; i < n ;i ++)
        {
            scanf("%d",&a[i]);

        }

        int num = 0;
        for(int i = 0 ;i < n ; i ++)
        {
            int minn = a[i] ;
            int k = i;
            for(int j = i + 1 ; j < n ; j ++)
            {
                if(minn > a[j])
                {
                    minn = a[j];
                    k = j;
                }
            }
            if(k == i)
                continue;
            else
            {
                swap(a[i] , a[k]);
                t1[num] = i;
                t2[num++] = k;
            }
        }
        printf("%d\n",num);
        for(int i = 0 ; i < num ; i ++)
        {
            printf("%d %d\n" , t1[i] , t2[i]);
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: