UVa 331 Mapping the Swaps
2014-03-20 19:36
323 查看
Mapping the Swaps |
in the sequence they are to be swapped, we obtain what might be called a swap map. For example, suppose we wish to sort the array A whose elements are 3, 2, and 1 in that order. If the subscripts for this array are 1, 2, and 3, sorting the array can be accomplished
by swapping A2 and A3, then swapping A1 and A2, and finally swapping A2 and A3. If a pair is identified in a swap map by indicating the subscript of the first element of the pair to be swapped, then this sorting process would be characterized with the swap
map 2 1 2.
It is instructive to note that there may be many ways in which swapping of adjacent array entries can be used to sort an array. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then
swapping A2 and A3, and finally swapping A1 and A2 again. The swap map that describes this sorting sequence is 1 2 1.
For a given array, how many different swap maps exist? A little thought will show that there are an infinite number of swap maps, since sequential swapping of an arbitrary pair of elements will not change the order
of the elements. Thus the swap map 1 1 1 2 1 will also leave our array elements in ascending order. But how many swap maps of minimum size will place a given array in order? That is the question you are to answer in this problem.
Input
The input data will contain an arbitrary number of test cases, followed by a single 0. Each test case will have a integer n that gives the size of an array, and will be followed by the n integervalues in the array.
Output
For each test case, print a message similar to those shown in the sample output below. In no test case willn be larger than 5.Sample Input
2 9 7 2 12 50 3 3 2 1 3 9 1 5 0
Sample Output
There are 1 swap maps for input data set 1. There are 0 swap maps for input data set 2. There are 2 swap maps for input data set 3. There are 1 swap maps for input data set 4.
不做这道题可能我一直意识不到冒泡排序时交换次数最少的排序法方法,虽然很容易想明白,但以前从来没想过。。每次递归模拟冒泡,遇到能排好序就ans++,因为时冒泡,所以ans必定是题目要求的最少次数,只需计算出ans多少就可以了,第一次递归前先进行判断数组是否已经有序,再选择是否递归。此题了加深对dfs递归思路的理解。新手做题,多多包涵。
#include<stdio.h> #include<string.h> int n,a[10],ans,k=1; void swap(int *x,int *y) { int temp=*x;*x=*y;*y=temp; } int ok() { for(int i=0;i<n-1;i++) if(a[i]>a[i+1]) return 0; return 1; } void dfs() { if(ok()) { ans++; return; } else for(int i=0;i<n-1;i++) { if(a[i]>a[i+1]) { swap(&a[i],&a[i+1]); dfs(); swap(&a[i],&a[i+1]); } } } int main() { while(~scanf("%d",&n) && n) { memset(a,0,sizeof(a)); for(int i=0;i<n;i++) scanf("%d",&a[i]); ans=0; if(!ok()) dfs(); printf("There are %d swap maps for input data set %d.\n",ans,k); k++; } return 0; }
相关文章推荐
- cocos2d-x内存管理
- 【iOS】关于视图生命周期、NSString变CGPath等问题
- android Systemproperties数据读写
- android apk 应用 重新签名 方法
- Android各种访问权限Permission详解
- Android TraceView工具使用
- Android通过NDK获取Keystore签名值
- 怎么样将自己开发的Android应用程序编译到系统Image中
- 新浪微博Android客户端开发
- this application requires.net framework4.0,please install the.net framework then run this installer
- 构建erlang的app
- 使用Unity3d的Physics.Raycast()的用法做子弹射击
- android调用第三方地图(URI)
- 每天一个小程序(5)——栈的应用数制转换
- (转)Android动画学习笔记-Android Animation
- Objective-c内存管理
- cocos2dx之lua派生类和方法重新
- activity启动的两种方
- Codeforces Round #136 (Div. 2) A. Little Elephant and Function
- 在 Android 的 IM 应用中使用 asmack 库实现用户头像的传输(基于VCard协议)