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的代码例如以下:

时间: 2025-01-05 06:28:17

UVA Mapping the Swaps的相关文章

uva 331 Mapping the Swaps (回溯)

uva 331 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 sequ

UVA - 331

 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 t

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

487--3279 UVA 755 其实有三种解法

说说:这道题初看挺简单的,题意无非就是将一串字符转化成一个用'-'隔开的电话号码,然后把出现超过一次的号码按照字典升序输出即可.但是这样样做是会超时的....其实把电话号码中间的'-'去掉,电话号码其实就是一个整数,有了这个想法那就简单啦.只要设立一个超大的数组包含所有可能的电话号码,然后数组的值是该号码出现的次数,统计完后遍历一遍输出即可.但是第三种相当于把前两种方法法结合起来了.把号码当成一个整数,但是号码存储在一个数组中,号码出现的次数存储在另一个数组中.这样在插入新号码的时候就排序,然后

uva 10312 - Expression Bracketing(Catalan+SuperCatalan)

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

UVA - 10716 - Evil Straw Warts Live (简单模拟)

UVA - 10716 Evil Straw Warts Live Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an

uva 755

题目 Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes onl

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te