浙大PAT-Sort with Swap(0, i)
来源:互联网 发布:云计算认证考试 编辑:程序博客网 时间:2024/06/07 00:23
来自ZJU数据结构基础的习题。
Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:
Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N(≤105) followed by a permutation sequence of {0, 1, …, N−1}. All the numbers in a line are separated by a space.Output Specification:
For each case, simply print in a line the minimum number of swaps need to sort the given permutation.Sample Input:
10
3 5 7 2 6 4 9 0 8 1Sample Output:
9
题目意思是,给定一串由0,1,2,…,N-1重新排列而成的数,只通过交换0与某一个数这样的操作,最后将数列排列为0,1,2,…,N-1。让你编一个程序,接收给定的数列,输出排列结束所需最少的交换次数。C++代码我放在最后,这里撇开程序,集中讨论解决这个问题的方法。
设
用表格记录置换如下:
把表格中规定的对应关系记为
现在对于
举个例子:现在
从0开始构造数据链:
0->3->2->7->(0)
1->5->4->6->9->(1)
8->(8)
在同一个链中的元素,就认为是互相等价的。那么在此例中,0到
现在我们从等价类的角度来看一般的交换操作。设swap(i,j)
表示交换数据1->5->4->6->9->(1)
中,交换1和6,那么新的1->9->(1)
,类似地,包含6的链为6->5->4->(6)
。如果swap(i,j)
导致两条链合并成一条。例如0->3->2->7->(0)
与1->5->4->6->9->(1)
,交换7和5,链变成7->4->6->9->1->5->0->3->2->(7)
。
我们最终的目的,是要通过0与其他元素的交换,将0到
事实上这个下界可以用这样的方法取到:将0所在链断成若干个单元素等价类,然后将0元素与其他非单元素链合并,再重复上述操作,除非找不到其他单元素链,这时任务完成。实际上,这就是代码所蕴含的思路。
以示例为例:
0->3->2->7->(0)
1->5->4->6->9->(1)
8->(8)
交换0与3(交换1次),得到
0->2->7->(0)
1->5->4->6->9->(1)
8->(8)
3->(3)
依次交换0与2,7(累计交换3次),得到
0->(0)
1->5->4->6->9->(1)
8->(8)
3->(3)
2->(2)
7->(7)
也就是说,依次将0与
交换0与1(累计交换4次),得到
0->5->4->6->9->1->(0)
8->(8)
3->(3)
2->(2)
7->(7)
这一操作把单元素链0与其他非单元素链组合起来。
再依次交换0与5,4,6,9,1(累计交换9次),即可完成任务。这样一来,交换次数为9,即为所求。
下面是该问题的C++代码解,时间和空间复杂度都是O(n)。注释我就没写了,因为网上可以搜到相同思路的代码,只是没有像上面一样给出一个比较完善的证明。
#include <iostream>using namespace std;int search(int* p, int num){ static int first = 1; for (int i = first; i<num; i++) if (p[i] != i) return first = i; return 0;}void swap(int* p, int n){ int temp = p[0]; p[0] = p[n]; p[n] = temp;}int main(){ int n; cin >> n; int *data = new int[n]; int temp; for (int i = 0; i<n; i++) { cin >> temp; data[temp] = i; } int count = 0; while (true) { if (data[0]) swap(data, data[0]); else { temp = search(data, n); if (!temp) break; swap(data, temp); } count++; } cout << count << endl; delete[] data; return 0;}
- 浙大PAT-Sort with Swap(0, i)
- pat--Sort with Swap(0, i)
- 浙大PAT 1067题 1067. Sort with Swap(0,*)
- Sort with Swap(0, i)
- Sort with Swap(0, i)
- 【PAT】1067. Sort with Swap(0,*)
- pat 1067. Sort with Swap(0,*) (25)
- PAT 1067 Sort with Swap(0,*)
- PAT 1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*) (25)
- pat 1067 Sort with Swap(0,*) (25)
- PAT 1025Sort with Swap(0,*) (25)
- PAT|1067. Sort with Swap(0,*)
- pat-1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*)
- pat-a1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*)
- PAT A1067. Sort with Swap(0,*) (25)
- NSUserDefaults
- 笑傲IT职场
- AsyncTask的用法
- 论如何使用Python进行微信公众号的开发
- AngularJS SQL
- 浙大PAT-Sort with Swap(0, i)
- angularjs的单选框+ng-repeat的实现
- 西蒙iphone-OpenGL ES 教程-01 : 构成复杂物体的基本绘图要素
- Android之原生项目集成React Native
- JQuery Mobile 知识点与实例演示Android手机APP
- PHP在windows下配置pgsql或pdo_pgsql扩展(找不到libpq.dll的报错解决方法)
- angular-ui-tree使用简介
- 高德地图的调试版SHA1与发布版SHA1分别的获取,申请到KEY
- unity5.0AssetBundle学习