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 integernthat gives the size of an array, and will be followed by theninteger
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 willnbe 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.
又是回溯,求交换排序的不同方案次数。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int arry[10];
int n,s;
bool sorted()
{
for(int i=0;i<n-1;i++)
{
if(arry[i]>arry[i+1])
return false;
}
return true;
}
void dfs()
{
if(sorted())
s++;
else
{
for(int i=0;i<n-1;i++)
{
if(arry[i]>arry[i+1])
{
swap(arry[i],arry[i+1]);
dfs();
swap(arry[i],arry[i+1]);
}
}
}
}
int main()
{
int t=0;
while(cin>>n&&n)
{
int i,j,k;
for(i=0;i<n;i++)
cin>>arry[i];
s=0;
if(!sorted())
dfs();
cout<<"There are "<<s<<" swap maps for input data set "<<++t<<"."<<endl;
}
return 0;
}
分享到:
相关推荐
delphi 2010升级到xe8后,decodestring汉字出现:No mapping for the.mht
Title:3D Robotic Mapping The Simultaneous Localization and Mapping Problem with Six Degrees of Freedo ISBN-13 书号:9783540898832 Author 作者:Nuchter, Andreas 出版社:Springer Publication Date 出版日期...
Experimental results demonstrate the efficiency and precision of the proposed method by mapping the Ada Byron building at our campus. We also analyze, using simulations, the precision and convergence...
一、solidity中,映射的关键字为mapping,首先我们先来定义两个mapping, mapping(address =>uint) idmapping和mapping(uint =>string) namemapping。idmapping用来表示地址变量和整型变量的对应关系,在注册过程中...
在SAP Process Integration (PI) 中,Java Mapping是一种强大的工具,用于处理和转换数据流,以确保不同系统间的数据交换准确无误。标题提到的"com.sap.aii.mapping.api PI MAPPING开发必须jar包"是Java Mapping开发...
Generic Mapping Tools (GMT) 中文手册 Generic Mapping Tools (GMT) 是一个功能强大且灵活的开源软件,用于创建地图、可视化和处理地理空间数据。GMT 的中文手册旨在帮助用户快速入门、安装和使用 GMT 软件。 GMT...
文档《deeper understanding of the homography mapping.pdf》详细讲解了单应性矩阵的详细推导过程及求解方法,这是一份宝贵的学术资源,由Ezio Malis和Manuel Vargas共同撰写。 文档中提到的关键知识点包括: 1. ...
端口转换软件TcpMapping是一款非常实用的工具,主要用于网络中的端口映射或者称为端口转发。在深入了解TcpMapping之前,我们首先需要理解什么是端口和端口转换。 端口在计算机网络中扮演着重要的角色,它们是网络...
《MATLAB Mapping Toolbox详解》 MATLAB Mapping Toolbox是MATLAB软件环境中的一个重要工具箱,专为地球科学、航空航天、地理信息系统(GIS)以及导航领域的研究人员和工程师设计。它提供了丰富的函数和工具,用于...
SANGFOR NGAF 6.8 DNS-Mapping 配置指导 SANGFOR NGAF 6.8 DNS-Mapping 配置指导是深信服公司发布的一份关于 NGAF 6.8 version 的 DNS-Mapping 配置指南。下面是从该指导中总结的重要知识点: 1. 文档说明:文档...
XI PI MAPPING开发必须jar包 import com.sap.aii.mapping.api.*; import com.sap.aii.mapping.api.*; import com.sap.aii.mapping.lookup.*; import com.sap.aii.mappingtool.tf7.rt.*;
GraphSLAM算法是一种用于解决大规模地图构建问题的统一算法,特别适合离线SLAM(同时定位与地图构建)问题。GraphSLAM将SLAM的后验概率转化为图形网络,代表数据的对数似然性。然后,使用变量消去技术来简化这个图形...
《MATLAB Mapping Toolbox详解——基于R2019b版本》 MATLAB Mapping Toolbox是MATLAB软件中的一个重要扩展工具箱,专为地球科学、航空航天、地理信息系统(GIS)以及地图制图领域的用户设计。该工具箱包含了丰富的...
标题 "Indoor-Mapping-Using-the-VLC-Channel-State-Information-master源码" 提供的信息表明,这是一个关于室内定位技术的项目,它利用了Visible Light Communication (VLC)的信道状态信息。VLC是一种利用可见光...
arcgis103.1版本插入动态表格,制图时插入动态表格,Esri Mapping and Charting Solutions。
excel导入mapping设置
根据文件提供的信息,这篇论文的标题为《Hybrid mapping service for the separation solutions of routing scalability》(《路由可扩展性分离方案的异构映射服务》),其内容主要围绕当前互联网路由表快速增长所...
arcgis10.2.2动态表格扩展模块,mapping and charting solutions
ArcGIS Production Mapping 10.1扩展,动态图表,百度网盘链接永久有限。