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"> < ...
随机推荐
- c# winform 隐藏tabcontrol标签
Apperarance 属性:Faltbuttons SizeMode属性:Fixed 各个TabPage的Text :空 ItemSize : Width=0;Height=1;
- Android定时器实现方法[转]
秒,单位毫秒Message message=new Message();message.what=1;handler.sendMessage(message);//发送消息} catch (Inter ...
- uva_1422 Processor
题目链接 题意: 有n个任务, 每个任务要在规定的时间[l, r]内完成, 工作量为 w, 每个任务可以分开完成. 求, 使得所有任务都完成的最大速度的最小值. 思路: 最大值最小问题, 二分. 因为 ...
- C# 两个获得程序运行路径的函数
EXE文件的存储路径,不太受调用时环境变量的影响: Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Lo ...
- ImageButton和Button区别
一.基础准备 Imagebutton 继承 Imageview,就是用一个图标代表了一些文字,它没Android:text属性.它由Android:src指定图标的位置 android:s ...
- Stanford CoreNLP--功能列表
Standford CoreNLP包含很多功能,github上有源码,github地址:Stanford CoreNLP,有需要的话可以下载看看. 主要内容在网站上都有描述,原文是这样写的: Choo ...
- IntelliJ IDEA SVN的账号修改 信息清除
来到编译器的setting设置 搜索subversion 点击subversion 找到下面的clear auth...按钮,点击一下 就可以了
- VS2005 检测内存泄漏的方法(转载)
一.非MFC程序可以用以下方法检测内存泄露: 1.程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __F ...
- 远程仓库版本回退方法 good
1 简介 最近在使用git时遇到了远程分支需要版本回滚的情况,于是做了一下研究,写下这篇博客. 2 问题 如果提交了一个错误的版本,怎么回退版本? 如果提交了一个错误的版本到远程分支,怎么回退远程分支 ...
- redis的图形界面管理工具:phpredisadmin
大部分人都知道redis是一款用在缓存服务器上的软件,它与memcache类似,都可以存储海量的数据,用在大访问量的web网站.聊天记录存放等方面,但是又与memcache不同: 1.缓存数据可以持久 ...