UVA Mapping the Swaps
题目例如以下:
Mapping the Swaps |
Sorting an array can be done by swapping certain pairs of adjacent entriesin the array. This is the fundamental technique used in the well-knownbubble sort. If we list the identities of the pairs to be swapped, in thesequence 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 A1and
A2, and finally swapping A2 and A3. If a pair is identified in a swapmap by indicating the subscript of the first element of the pair to beswapped, then this sorting process would be characterized with the swap map2 1 2.
It is instructive to note that there may be many ways in which swapping ofadjacent 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 swappingA2 and A3, and finally swapping A1
and A2 again. The swap map that describesthis sorting sequence is 1 2 1.
For a given array, how many different swap maps exist?
A little thoughtwill show that there are an infinite number of swap maps, since sequentialswapping 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 arrayelements in ascending order. But how many swap maps of minimum size willplace 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 asingle 0. Each test case will have a integer
n that gives the size of anarray, and will be followed by the n integer values in the array.
Output
For each test case, print a message similar to those shown in thesample output below. In no test case will
n 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.
求将一个数组排序的不同方法的个数,能够将数组相邻两数交换。要求交换的次数必须最少。事实上仅仅要不反复交换两个元素。交换的次数就是最少的。知道这一点后就能够直接回溯了。
AC的代码例如以下:
UVA Mapping the Swaps的更多相关文章
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- uva 331 Mapping the Swaps 求交换排序的map 纯DFS
给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序. n不大于5,用dfs做. 代码: #include <cstdio> #include <cstring> # ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- 使用MyBatis Generator自动创建代码(dao,mapping,poji)
连接的数据库为SQL server2008,所以需要的文件为sqljdbc4.jar 使用的lib库有: 在lib库目录下新建一个src文件夹用来存放生成的文件,然后新建generatorConfig ...
- Nhibernate mapping 文件编写
生成工具软件 现在生成工具软件有很多了,例如商业软件:NMG.CodeSmith.Visual NHibernate,开源软件:MyGeneration.NHibernate Modeller.AjG ...
- mybatis generator.xml 配置 自动生成model,dao,mapping
generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...
- Spring MVC --->>>No mapping found for HTTP request with URI
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < ...
随机推荐
- uboot start.S分析
一.概述 1.本文综述及特色 阅读uboot,start.S是第一个源程序文件,主要完成初始化看门狗.定时器.重定位(拷贝代码段到内存中).初始化堆栈.跳转到第二阶段等工作. 网上关于这些内容的 ...
- HTML DOM Select 对象
Select 对象 Select 对象代表 HTML 表单中的一个下拉列表. 在 HTML 表单中,<select> 标签每出现一次,一个 Select 对象就会被创建. 您可通过遍历表单 ...
- aircrack-ng on OSX 从零开始之探测
继续上一篇内容,在安装好aircrack-ng之后,就要学习如何对目标进行探测了.找了篇教程跟着学习一下吧.其实网上关于使用aircrack-ng的教程还是很多的,我也参考了很多,不过最后还是以官方的 ...
- Cloud Insight 现在已经支持监控 Cassandra 啦!
Cassandra 是什么? Apache Cassandra 以其可扩展性和容错分布式数据库系统而被人所熟知.Cassandra 起源于Facebook 最初创建于 Amazon Dynamo 和谷 ...
- Android 向系统添加一个联系人信息contact
private void writeContacts() { Uri rawContacts = Uri.parse("content://com.android.contacts/raw_ ...
- Chapter 32:动态规划一 总结
今天做了四道dp题,分数306. 就从我的做题顺序开始讲.. 首先是第四题,明显的记录模数DP,很快打出来了. 后来做完之后回来拍第四题,也没有什么问题,就AC了. 然后是第三题,我的性子真是太急了, ...
- 【POJ 1830】 开关问题 (高斯消元)
开关问题 Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为 ...
- PorterDuff及Xfermode初识
图像合成,是将两幅退昂放在一起的动作,它使得我们能够同时看到两幅图像的特征. 我们可以首先在Canvas对象上绘制一个位图对象,然后再相同的Canvas对象上绘制第二个位图对象的方式来实现合成.不过这 ...
- linux操作Oracle导入导出dmp数据命令
--清空该表数据 :非索引清空TRUNCATE TABLE GE_INTERFACE_MESSAGE; --数据库导出 指定导出某个用户的所有数据 包括表 索引 序列 存储过程 函数 等exp CX ...
- What exactly is the difference between WndProc and DefaultWndProc?
Control.DefWndProc Sends the specified message to the default window procedure. 参数说明:m:The Windows M ...