您的位置:首页 > 其它

uva 1611 Crane

2016-01-06 20:46 411 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: