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次,这样是满足条件的。
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; }
相关文章推荐
- MyBatis传入参数与parameterType
- HDOJ 2013 蟠桃记
- MapReduce 图解流程超详细解答(2)-【map阶段】
- Pascal's Triangle II leetcode
- NSString基础2-判断前缀、后缀、分割、截取、拼接、查询
- bzoj1023: [SHOI2008]cactus仙人掌图
- 批处理:如何逐行读取txt中每行的信息并显示
- Android 三种常用XML解析方式:SAX,DOM和PULL
- leetcode第一刷_Maximum Depth of Binary Tree
- UITextField的扩展逻辑
- LoadRunner运行场景,错误---Error: Failed to send data by channels - post message failed.
- 【技术探究】持续集成
- 翻页效果
- 蓝牙防丢
- Android开源项目分类汇总--转载
- JavaScript escape() 函数详解
- Android 用state list drawable动态改变按钮点击前后的状态
- 二叉树的一些基础算法(先序,中序,后序,层次,深度,宽度,距离)
- 【CODEVS1191】数轴染色
- windows应用程序,操作系统,计算机硬件之间的相互关系———消息队列