HDU 1027 Ignatius and the Princess II 排列生成
解题报告: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 排列生成的更多相关文章
- 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 ...
- HDU - 1027 Ignatius and the Princess II 全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 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 ( ...
- HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- 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 ...
- HDU 1027 Ignatius and the Princess II 选择序列题解
直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...
- HDU 1027 - Ignatius and the Princess II
第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...
- hdoj 1027 Ignatius and the Princess II 【逆康托展开】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
随机推荐
- python基础(六)python操作excel
一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...
- mysql 数字类型的长度区别
mysql整型bigint.int.mediumint.smallint 和 tinyint的语法介绍,如下: 1.bigint 从 -2^63 (-9223372036854775808) 到 2^ ...
- Windows10 桌面显示 我的电脑
桌面上面有我的电脑还是非常方便的 但是 不激活有时候没法选择: 方法 桌面空白处右键---个性化 然后在 个性化- 主题- 桌面图标设置 增加即可.
- sublime 对vue的高亮显示
前提概要: sublime的下载地址:http://www.sublimetext.com/ notepad++下载地址:https://notepad-plus-plus.org/ .vue的文件在 ...
- UpdateBatch与事务处理的一点总结
对于某些设置或者提交,如果希望是全部修改完毕后才确定生效,可以将DataSet的LockType:=ltBatchOptimistic,这样就可以了.然后每次做的修改和改动都暂时保存在缓存中,必须通过 ...
- 题解 P4379 【[USACO18OPEN]Lemonade Line】
不敢快速排序又想要快排的速度,还不用STL的小伙伴们看这里! 小金羊终于学会了堆排以外的另外的一种排序 (打个题解巩固一下) 归并排序(mergesort): 时间复杂度和快排一样的优秀. 先说归并排 ...
- java链表的各种操作
java里面没有指针的说法,所以初始化的时候,就是新建一个null节点就是一个空链表了.//C里面链表会有头指针,头指针指向头节点 如果想向空链表插入第一个节点,直接head=newNode: 注意的 ...
- expect ssh 自动登录 example
#!/usr/bin/expect -f set ip [lindex $argv ] set port [lindex $argv ] set username [lindex $argv ] se ...
- C++ STL 常用拷贝和替换算法
C++ STL 常用拷贝和替换算法 copy() 复制 vector<int> vecIntA; vecIntA.push_back(1); vecIntA.push_back(3); v ...
- 【BZOJ1048】分割矩阵(记忆化搜索,动态规划)
[BZOJ1048]分割矩阵(记忆化搜索,动态规划) 题面 BZOJ 洛谷 题解 一个很简单的\(dp\),写成记忆化搜索的形式的挺不错的. #include<iostream> #inc ...