Mapping the Swaps

Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs to be swapped, 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 integer values 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.

 

 

题意: 求通过交换相邻元素排序,求最少交换次数的方法有几种。

最少交换次数就是逆序对数,枚举交换方案即可。

另外,用冒泡排序的方法一定是交换次数最少的方案。

 

解法一: 枚举逆序对数减少的方案,到达有序序列的时候,就一定是最少交换次数的方案。 time: 0.015

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 10;
int n;
int a[maxn];
int ans;
bool isorder()
{
for(int i=0;i<n-1;i++) if(a[i]>a[i+1])
return false;
return true;
} void dfs(int d)
{
if(isorder())
{
ans++; return;
} for(int i=0;i<n-1;i++) if(a[i]>a[i+1])
{
swap(a[i], a[i+1]);
dfs(d+1);
swap(a[i], a[i+1]);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva331.in", "r", stdin);
#endif
int kase=0;
while(scanf("%d", &n)==1 && n)
{
for(int i=0;i<n;i++) scanf("%d", a+i);
ans=0;
if(!isorder())
dfs(0);
printf("There are %d swap maps for input data set %d.\n", ans, ++kase);
} return 0;
}

解法二: 先求出逆序对数d,然后暴力枚举排序方案(控制深度为d),如果到达深度d时,序列变成有序序列,就找到一个最少交换次数的方案。 time: 0.059

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10;
int n;
int inv, a[maxn], ans;
void dfs(int d)
{
if(d==inv) {
for(int i=0;i<n-1;i++) if(a[i]>a[i+1]) return;
ans++;
} else for(int i=0;i<n-1;i++) {
swap(a[i], a[i+1]);
dfs(d+1);
swap(a[i], a[i+1]);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva331.in", "r", stdin);
#endif
int kase = 0;
while(scanf("%d", &n) == 1 && n) {
for(int i=0;i<n;i++) scanf("%d", &a[i]);
inv=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j]) inv++;
ans=0;
if(inv > 0) dfs(0);
printf("There are %d swap maps for input data set %d.\n", ans, ++kase);
} return 0;
}

uva331 - Mapping the Swaps的更多相关文章

  1. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  2. uva 331 Mapping the Swaps 求交换排序的map 纯DFS

    给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序. n不大于5,用dfs做. 代码: #include <cstdio> #include <cstring> # ...

  3. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  4. 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 ...

  5. 使用MyBatis Generator自动创建代码(dao,mapping,poji)

    连接的数据库为SQL server2008,所以需要的文件为sqljdbc4.jar 使用的lib库有: 在lib库目录下新建一个src文件夹用来存放生成的文件,然后新建generatorConfig ...

  6. Nhibernate mapping 文件编写

    生成工具软件 现在生成工具软件有很多了,例如商业软件:NMG.CodeSmith.Visual NHibernate,开源软件:MyGeneration.NHibernate Modeller.AjG ...

  7. mybatis generator.xml 配置 自动生成model,dao,mapping

    generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...

  8. Spring MVC --->>>No mapping found for HTTP request with URI

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < ...

  9. elasticsearch 之mapping

    搭好elk 后,有时候会发现kibana 统计数据时,数据类型不对,这个时候就和elasticsearch的mapping有关,虽然我们可以用logstash修改传入es里的数据类型,比如 float ...

随机推荐

  1. python定义影像投影

    import os import arcgisscripting gp=arcgisscripting.create() coordsys=r"C:\Winx86\ArcGIS\Coordi ...

  2. [转] C#中绘制矢量图形

    无涯 原文 C# 绘制矢量图形 [原创] 近来参与了一个项目,软件主要的功能就是使用C#画矢量图,然后导出到Word.Excel.Powerpoint中,并且能够再次被编辑.以下是我们的解决过程: 首 ...

  3. Net判断一个对象是否为数值类型 z

    http://www.cnblogs.com/SkyD/p/4053461.html public static bool IsNumeric(this Type dataType) { if (da ...

  4. 实测 windows下nginx很不稳定,换成squid好多了

    在windows server 2003 下尝试了号称支持50,000个并发连接数响应的nginx,结果相当不理想. 具体表现为时不时网页就卡住不动了,需要再刷新一次网页才能显示,而服务器cpu和内存 ...

  5. 网页元素定位神器之Xpath详解

    摘要: 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下. ...     ...

  6. 性能测试之LoardRunner 测试场景监控关注的几点

    1.系统业务处理能力,即通常我们在进行性能测试的时候,在特定的硬件和软件环境下考察的业务处理能力,即“事物”,需要关注当前.平时.峰值以及长远未来业务发展情况,考虑不同业务的处理数量,从而设定相应的业 ...

  7. eclipse 文本编辑器

    Eclipse文本编辑器拥有编辑器的标准功能,包括数目不限的Undo(Ctrl+Z)和Redo(Ctrl+Y)操作.使用快捷键Ctrl+F后,会出现Find/Replace对话框,快捷键Ctrl+K或 ...

  8. ansible命令执行模块使用

    ansible命令执行模块使用 1.命令执行模块-command 在远程节点上运行命令. 命令模块使用命令名称,接上空格-的分割符作为参数使用,但是不支持管道符和变量等,如果要使用这些,那么可以使用s ...

  9. (转)android中利用 ViewPage 实现滑动屏

    最近实现了这样的一个效果:滑动界面出现拖拽效果,可翻动3屏,也可点击按钮翻动页面. 主要利用android.support.v4.view.ViewPager控件来实现. 第一个界面: 滑动屏幕: 换 ...

  10. BITED数学建模七日谈之四:数学模型分类浅谈

    本文进入到数学建模七日谈第四天:数学模型分类浅谈 大家常常问道,数学模型到底有哪些,分别该怎么学习,这样能让我们的学习有的放矢,而不至于没了方向.我想告诉大家,现实生活中的问题有哪些类,数学模型就有哪 ...