大意;求在最小的交换次数的情况下使得序列升序排列的方案数。

思路:冒泡排序对应最小交换数,然后回溯。

CODE:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;

int ans;
int n;
int a[1001];

int check(int *a)
{
    for(int i = 0; i < n-1; i++)
    {
        if(a[i] > a[i+1]) return 0;
    }
    return 1;
}

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

void dfs(int *a)
{
    if(check(a))
    {
        ans++;
        return ;
    }
    else for(int i = 0; i < n-1; i++)
    {
        if(a[i] > a[i+1])
        {
            swap(a[i], a[i+1]);
            dfs(a);
            swap(a[i], a[i+1]);
        }
    }
}

int main()
{
    int times = 0;
    while(~scanf("%d", &n) && n)
    {
        ans = 0;
        for(int i = 0; i < n; i++) scanf("%d", &a[i]);
        if(!check(a))
        {
            dfs(a);
        }
        printf("There are %d swap maps for input data set %d.\n", ans, ++times);
    }
    return 0;
}

转载于:https://www.cnblogs.com/g0feng/archive/2012/10/13/2722444.html

UVA 331 Mapping the Swaps相关推荐

  1. 331 - Mapping the Swaps(回溯+ dfs)

    题目:331 - Mapping the Swaps 题目大意:给出一个序列,要求只能相邻的进行交换,求交换次数最少的不同的交换方式有多少种. 解题思路:每次都可以从头选两个需要交换的位置进行交换,( ...

  2. UVA Mapping the Swaps

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

  3. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  4. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  5. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

  6. 初学者acm的练习题指南

    上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1.    <!--[endif]-->Programming Bas ...

  7. uva 10256 The Great Divide (Convex Hull, Simple)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=sh ...

  8. uva 299 - Train Swapping

    2019独角兽企业重金招聘Python工程师标准>>> 题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudg ...

  9. UVa 207 - PGA Tour Prize Money

    时间限制:3.000秒 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&catego ...

最新文章

  1. 肿瘤细胞信号转导: 肿瘤治疗的靶向信号通路 PDF
  2. javafx 调用java_Java,JavaFX的流畅设计风格进度栏
  3. 【渝粤教育】电大中专药物分析技术基础 (2)作业 题库
  4. python与android交互,Android客户端与Python服务器端的简单通信
  5. 【转载】ORM的概念
  6. 蓝桥杯 ALGO-124 算法训练 数字三角形
  7. 三极晶体管放大电路实验
  8. 什么时候不需要回表?
  9. iOS:懒加载符号绑定流程
  10. python 3d游戏编程入门_用python写游戏 - 从入门到精通16
  11. 行业分类代码 2017 最新版 创建sql语句
  12. java用下划线分开字母和数字_数字文字中的Java 7下划线
  13. Python中不为人知的四个特性
  14. Eclipes和Myeclipse插件安装方法
  15. python函数可以分为哪几类_python函数有哪几种
  16. 这届年轻人,怎么开始想着吃零食减肥了?
  17. 数据库的基本概念-基础(课堂笔记)
  18. 从win+ubuntu双系统安装到ubuntu环境配置和美化
  19. mybatis模仿1之我先看看
  20. 电商软件性能测试,实战 | 电商业务的性能测试(一): 必备基础知识

热门文章

  1. 多目标跟踪笔记二:Efficient Algorithms for Finding the K Best Paths Through a Trellis
  2. 自定义组件 点击空白处隐藏
  3. Python——反射
  4. shell之实战应用一(查找xml文档中的关键字段)
  5. JSON中的安全问题
  6. Spring中自己主动装配
  7. Mybatis实现物理分页
  8. mysql数据库主要用来做什么的_为什么有了 MySQL ,还要有时序数据库?
  9. 计算机网络-TCP拥塞控制
  10. 【Linux 内核】Linux 内核体系架构 ( 进程调度 | 内存管理 | 中断管理 | 设备管理 | 文件系统 )