UVa 331:Mapping the Swaps

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267

题目类型: 回溯

原题:

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.

题目大意:

给定一个数字序列,然后要你用交换相邻两个数的方法, 用最少交换次数进行排序的方案数有多少种

分析与总结:

首先,用冒泡排序的方法一定是交换次数最少的方案。那么只要进行简单的暴力搜索出所有交换方案即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 7
using namespace std;
bool vis[MAXN], flag;
int minSwap,arr[MAXN], n;  

void swap(int &a,int &b){int t=a; a=b; b=t;}  

inline bool isAccend(){
    for(int i=2; i<=n; ++i)
        if(arr[i] < arr[i-1]) return false;
    return true;
}  

void dfs(){
    if(isAccend()){
        ++minSwap;
        return;
    }
    for(int i=1; i<n; ++i){
        if(arr[i] > arr[i+1]) {
            swap(arr[i], arr[i+1]);
            dfs();
            swap(arr[i], arr[i+1]);
        }
    }
}  

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    int cas=1;
    arr[0] = -99999;
    while(scanf("%d", &n), n){
        for(int i=1; i<=n; ++i)
            scanf("%d", &arr[i]);
        minSwap=0;
        if(!isAccend())
            dfs();
        printf("There are %d swap maps for input data set %d.\n", minSwap, cas++);
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索array
, elements
, and
, The
, be
swap
,以便于您获取更多的相关知识。

时间: 2024-10-26 17:57:41

UVa 331:Mapping the Swaps的相关文章

UVa 331 Mapping the Swaps (DFS)

331 - Mapping the Swaps Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267 Sorting an array can be done by swapping certain pairs of adjacent entries in

Knockout应用开发指南 第七章:Mapping插件

原文:Knockout应用开发指南 第七章:Mapping插件 Mapping插件 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些属性是observable的,你可以使用KO绑定他们到你的UI元素上,当这些observable值改变的时候,这些UI元素就会自动更新. 绝大多数程序都需要从服务器端获取数据,但是由于服务器不知道observable的概念是什么,它只支持简单的JavaScript对象(通常是序列化以后的JSON),

NHibernate3剖析:Mapping篇之ConfORM实战(4):ManyToMany语义

ConfORM概述 如果你不熟悉ConfORM请查看前几篇文章,你可以到http://code.google.com/p/codeconform/获取ConfORM最新版本. 在Domain设计中经常使用集合,在.Net中的集合有四种:Iesi.Collections.Generic.ISet<T>.System.Collections.Generic.ICollection<T>.System.Collections.Generic.IList<T>.System.C

一起谈.NET技术,NHibernate3剖析:Mapping篇之ConfORM实战(4):ManyToMany语义

ConfORM概述 如果你不熟悉ConfORM请查看前几篇文章,你可以到http://code.google.com/p/codeconform/获取ConfORM最新版本. 在Domain设计中经常使用集合,在.Net中的集合有四种:Iesi.Collections.Generic.ISet<T>.System.Collections.Generic.ICollection<T>.System.Collections.Generic.IList<T>.System.C

uva 331 - Mapping the Swaps

点击打开链接 题目意思:给定一个无序的序列,要求找到一个交换次数最少的方案使得序列为有序列,然后再找到以该最少次数为方案的总数是多少,输出方案数 解题思路:根据冒泡排序的方法,我们将可以得到最少的交换次数,然后问我们在这个次数下的交换方式有几种.我们可以利用冒泡排序的思想,就是我们每一次递归下去的时候都从0这个点开始搜索,只要有递归就判断当前的序列是否排好序,如果序列排好那么ans++并且不再递归下去直接return,注意这里每一次递归回来还要把两个数交换回来 代码: #include <ios

UVa 10716: Evil Straw Warts Live

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1657 [原题] A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a

UVa 112:Tree Summing 二叉树构造, 二叉树路径和

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=48&mosmsg=Submission+received+with+ID+10280379 题目类型: 数据结构, 二叉树 加强版样例输入: 22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will