uva 1611 Crane

There are n crates waiting to be loaded onto a ship. The crates are numbered 1, 2, … , n, the numbers
determining the order of loading. Unfortunately, someone messed up the transit and the crates are
standing in a row in an arbitrary order. As there is only limited space in the dock area, you must sort
the crates by swapping some of them.
You are given a crane that works in the following way: you select a connected interval of crates of
even length. The crane then exchanges the first half of the interval with the second half. The order
inside both halves remains unchanged. Determine the sequence of crane moves that reorders the crates
properly.
The crane’s software has a bug: the move counter is a 9-based (not 10-based, as you might think)
integer with at most 6 digits. Therefore, the crane stops working (and has to be serviced) after
9
6 = 531441 moves. Your solution must fit within this limit.
Input
The first line of input contains the number of test cases T. The descriptions of the test cases follow:
Each test case starts with an integer n, 1 ≤ n ≤ 10000, denoting the number of crates. In the next
line a permutation of numbers {1, 2, … , n} follows.
Output
For each test case print a single line containing m — the number of swaps — followed by m lines
describing the swaps in the order in which they should be performed. A single swap is described by
two numbers — the indices of the first and the last element in the interval to be exchanged. Do not
follow the crane’s strange software design — use standard decimal numeral system.

解题思路:本题采用贪心的方法可以求解,我们首先安排i号元素,通过我们的规则我们最多通过两次交换操作,便可以将i元素放置在i位置处,因此对于所有的元素升序,我们操作的次数最多不超过2n次,这样是满足条件的。

#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
const int maxn = 10010;
int a[maxn], pos[maxn];
vector< pair<int, int> > res;
int T, n;
int ans;

inline void swap(int &a, int &b) {
    int t = a;
    a = b;
    b = t;
    return ;
}

void op(int l, int r) {
    for(int i = l, j = r; i < r; ++i, ++j) {
        swap(a[i], a[j]);
        pos[a[i]] = i;
        pos[a[j]] = j;
    }
    /*
    printf("Test:");
    for(int j = 1; j <= n; ++j) {
        printf("%d ", a[j]);
    }
    printf("\n");
    */
    return ;
}


int main() {

    //freopen("aa.in", "r", stdin);

    scanf("%d", &T);
    while(T--) {
        ans = 0;
        res.clear();
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            pos[a[i]] = i;
        }
        for(int i = 1; i <= n; ++i) {
            if(a[i] == i) continue;
            if(2*pos[i] - i - 1 <= n) {
                res.push_back(make_pair(i, 2*pos[i]-i-1));
                op(i, pos[i]);
                ans++;
            } else {
                if((pos[i] - i + 1) % 2 == 0) {
                    res.push_back(make_pair(i, pos[i]));
                    op(i, i + (pos[i]- i + 1) / 2);
                    res.push_back(make_pair(i, 2*pos[i]-i-1));
                    op(i, pos[i]);
                } else {
                    res.push_back(make_pair(i + 1, pos[i]));
                    op(i + 1, i + (pos[i]- i) / 2 + 1);
                    res.push_back(make_pair(i, 2*pos[i]-i-1));
                    op(i, pos[i]);
                }
                ans += 2;
            }
        }
        printf("%d\n", ans);
        for(int i = 0; i < ans; ++i) {
            printf("%d %d\n", res[i].first, res[i].second);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值