解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列。

第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很明显,如果暴力枚举的话,时间复杂度就是O(n*m),为10^7,所以可以通过。这题其实就是一个排列生成的题,排列生成有多种方法,这题的关键是当找到答案的时候,要及时退出暴力过程,要不然肯定TLE。

 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; int num,n,m,visit[],flag; void dfs(int* A,int deep)
{
if(flag) return ;
if(deep - == n)
{
num++;
if(num == m)
{
for(int i = ;i<=n;++i)
printf(i==? "%d":" %d",A[i]);
flag = ;
return ;
}
}
else
{
for(int i = ;i<=n;++i)
if(!visit[i])
{
A[deep] = i;
visit[i] = ;
dfs(A,deep+);
visit[i] = ;
}
}
} int main()
{
int A[];
while(scanf("%d%d",&n,&m)!=EOF)
{
num = flag = ;
memset(visit,,sizeof(visit));
dfs(A,);
printf("\n");
}
return ;
}

HDU 1027 Ignatius and the Princess II 排列生成的更多相关文章

  1. HDU 1027 Ignatius and the Princess II(求第m个全排列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/10 ...

  2. HDU - 1027 Ignatius and the Princess II 全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  3. HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  4. HDU 1027 Ignatius and the Princess II(康托逆展开)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  5. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  6. hdu 1027 Ignatius and the Princess II(正、逆康托)

    题意: 给N和M. 输出1,2,...,N的第M大全排列. 思路: 将M逆康托,求出a1,a2,...aN. 看代码. 代码: int const MAXM=10000; int fac[15]; i ...

  7. HDU 1027 Ignatius and the Princess II 选择序列题解

    直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...

  8. HDU 1027 - Ignatius and the Princess II

    第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...

  9. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

随机推荐

  1. chdir

    <?php //获得当前目录 echo getcwd(); echo "<br />"; //改变为 images 目录 chdir("images&q ...

  2. 微信小程序 功能函数 把数字1,2,3,4换成春,夏,秋,冬

    let season =‘1,2,3’; // console.log(season.length) if (season){ if (season.length==1){ seasonChe1=se ...

  3. 关于sizeof

    sizeof是求占用的内存空间的大小,并不是指数组长度.(strlen 的长度只适合char*类型) 例如. int a[10]={0}; 数组a的长度为sizeof(a)/sizeof(a[0])— ...

  4. jquery datatables 添加跳转到指定页功能

    项目中使用了jquery datatables 作为我们的数据表格组件,但是分页上没有跳转到指定页,需要自己重新写.解决方法如下: 在设置dataTables的默认属性里设置它的drawCallbac ...

  5. TreeMap源码剖析

    原文  http://blog.csdn.net/chdjj/article/details/38782221 主题 源码分析红黑树 注:以下源码基于jdk1.7.0_11 之前介绍了一系列Map集合 ...

  6. Codeforces 633C Spy Syndrome 2 | Trie树裸题

    Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...

  7. 基本数据结构 —— 堆以及堆排序(C++实现)

    目录 什么是堆 堆的存储 堆的操作 结构体定义 判断是否为空 往堆中插入元素 从堆中删除元素 取出堆中最大的元素 堆排序 测试代码 例题 参考资料 什么是堆 堆(英语:heap)是计算机科学中一类特殊 ...

  8. 洛谷 P4128 [SHOI2006]有色图 解题报告

    P4128 [SHOI2006]有色图 题目描述 如果一张无向完全图(完全图就是任意两个不同的顶点之间有且仅有一条边相连)的每条边都被染成了一种颜色,我们就称这种图为有色图.如果两张有色图有相同数量的 ...

  9. rsync命令比对文件及增量同步

    A fast,versatile,remote (and local) file-copying tool. rsync基于ssh协议实现高效率远程或本地文件复制,传输速度比scp快.复制文件时会比对 ...

  10. go数据类型之基本类型

    基本数据类型 数值型 整数 int 64位操作系统默认为int64,32位操作系统为int32,但是类型检查时时int int8 (byte 1字节) int16 (short 2字节) int32 ...