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 ( ...
随机推荐
- From 百度知道 SQLSERVER 字符集排序规则简单说明
https://zhidao.baidu.com/question/390314825002277485.html 学习一下, 以后说不定用得到. collate Latin1_General_CS_ ...
- hbase 多个过滤器组合(列表)
使用FilterList要保证过滤器的顺序需要使用List<Filter> private static void mutilFilterData() throws IOException ...
- 【agc006f】Blackout(神仙题)
[agc006f]Blackout(神仙题) 翻译 给定一个\(n*n\)的网格图,有些格子是黑色的.如果\((x,y),(y,z)\)都是黑色的,那么\((y,x)\)也会被染黑,求最终黑格子数量. ...
- BZOJ2436 [Noi2011]Noi嘉年华 【dp】
题目链接 BZOJ2436 题解 看这\(O(n^3)\)的数据范围,可以想到区间\(dp\) 发现同一个会场的活动可以重叠,所以暴力求出\(num[l][r]\)表示离散化后\([l,r]\)的完整 ...
- 【poj3133】 Manhattan Wiring
http://poj.org/problem?id=3133 (题目链接) 题意 $n*m$的网格里有空格和障碍,还有两个$2$和两个$3$.要求把这两个$2$和两个$3$各用一条折线连起来.障碍里不 ...
- 学习操作Mac OS 之安装工具组件
视频软件: MPlayerX 安装MySQL: 下载MySQL: https://dev.mysql.com/downloads/installer/ 设置环境变量:http://www.cnblog ...
- Android应用自动更新功能的实现!
Android应用自动更新功能的实现!http://blog.csdn.net/android_tutor/article/details/7015986 private static final i ...
- MVC 中@Html.DropDownListFor() 设置选中项 这么不好使 ? [问题点数:40分,结帖人lkf181]
http://bbs.csdn.net/topics/390867060 由于不知道错误原因在哪 我尽量把代码都贴出来吧:重点是:在 Controller 类里 我给 SelectListItem集合 ...
- SSH框架搭建问题总结
1.eclipse中tomcat配置是否正确?能否在网页中访问的到? 如何在eclipse中配置tomcat就不说了,我们看下问题,在网页上访问tomcat的地址,为什么出现404错误呢? 解决办法: ...
- RF parameter
There are primarily 3 features which can be tuned to improve the predictive power of the model : 说明: ...