解题报告: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. PAT 1054 求平均值

    https://pintia.cn/problem-sets/994805260223102976/problems/994805272659214336 本题的基本要求非常简单:给定N个实数,计算它 ...

  2. cmd命令行安装,删除Windows证书(certgmr的简单使用)

    在管理证书的时候需要用到certmgr工具. 在cmd中执行certmgr会弹出证书管理的工具,但是不能用命令行去管理证书,需要额外的工具 cermgr.exe:下载链接 https://pan.ba ...

  3. Pygame - Python游戏编程入门(0) 转

    博客刚开,想把最近学习的东西记录下来,算是一种笔记.最近打算开始学习Python,因为我感觉Python是一门很有意思的语言,很早以前就想学了(碍于懒),它的功能很强大,你可以用它来做科学运算,或者数 ...

  4. Android 布局类控件

    Android提供6种布局类的控件:LinearLayout.TableLayout.GridLayout.FrameLayout.RalativeLayout.AbsoluteLayout 网上搜到 ...

  5. 【bzoj5210】最大连通子块和 树链剖分+线段树+可删除堆维护树形动态dp

    题目描述 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块和. 其中,一棵子树的最大连通子块和指的是:该子树 ...

  6. 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  7. Vue使用SCSS进行模块化开发

    原文地址:http://www.cnblogs.com/JimmyBright/p/7761531.html 个人认为scss最大的好处就是能将css属性设置为变量,这样让css一键更换主题成为可能. ...

  8. module.exports 、 exports 和 export 、 export default 、 import

    1:commonjs规范 module.exports={a:10,b:20} var test=require('lib/test') console.log(test.a);console.log ...

  9. Xpack集成LDAP

    支持两种配置方式: The ldap realm supports two modes of operation, a user search mode and a mode with specifi ...

  10. 【bzoj2754】【scoi2012】喵星球上的点名

    题解们: 1.首先可以被很多暴力给搞过去:我以前也是这样水过去的 2.ac自动机 2.1 抽离fail树 对点名建自动机,建$fail$树的时候只保留询问节点: 对于一个喵,子串==在自动机里匹配到的 ...