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 ( ...
随机推荐
- 工作中常用到的Linux命令
ps: (ps的参数分成basic, list, output, thread, miscellaneous) (basic) -e / -A 显示所有进程 (output) -o 输出指定字段 ls ...
- BZOJ5319 JSOI2018列队(主席树)
显然集合后相对位置不变最优.主席树上二分向左和向右的分界点即可.注意主席树的值域.我怎么天天就写点一眼题啊. #include<iostream> #include<cstdio&g ...
- linux eclipse add desktop shortcut with root permission
gedit ~/.local/share/applications/opt_eclipse.desktop sudo apt-get install gksu [Desktop Entry] Type ...
- mysql测试索引在表中的作用
//未完成 参考书:(完成对缓存中执行计划的查看对比 P133~) Microsoft SQL Server 2008技术内幕:T-SQL查询 实验内容 单表中的索引使用 1.建表 create ta ...
- web接口测试中需要测试的几个点
本文导读: web接口测试用例要包括欲测试的功能.应输入的数据和预期的输出结果,只有在数据能正确流入.流出模块的前提下,其他测试才有意义.下面介绍在web测试接口时一些需要注意的点 1.接口返回 数据 ...
- LINQ 模糊搜索
IList<entity> ls = new List<entity>(); ls = (from k in ls where k.Name.Contains("sa ...
- Google题解
Kickstart2017 RoundB B.题意: 二维平面上有n个点, 每个点坐标(xi, yi), 权值wi, 问: 在平面上找一点p, 使得 Σwi*max(|X-xi|, |Y-yi|)最小 ...
- BZOJ 2742: [HEOI2012]Akai的数学作业
2742: [HEOI2012]Akai的数学作业 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 535 Solved: 226[Submit][S ...
- 在OpenShift上托管web.py应用
一.背景 最近在学习web.py,跟随官网的cookbook和code examples一路敲敲打打,在本地访问了无数遍http://0.0.0.0:8080/,也算是对web.py有了基本的认识.为 ...
- bootstrap.yml与application.yml的区别
说明:其实yml和properties文件是一样的原理,主要是说明application和bootstrap的加载顺序.且一个项目上要么yml或者properties,二选一的存在. Bootstra ...