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 (Java/Others)
Total Submission(s): 9458 Accepted Submission(s): 5532
our hero finds the door to the BEelzebub feng5166. He opens the door
and finds feng5166 is about to kill our pretty Princess. But now the
BEelzebub has to beat our hero first. feng5166 says, "I have three
question for you, if you can work them out, I will release the Princess,
or you will be my dinner, too." Ignatius says confidently, "OK, at
last, I will save the Princess."
"Now I will show you the first
problem." feng5166 says, "Given a sequence of number 1 to N, we define
that 1,2,3...N-1,N is the smallest sequence among all the sequence which
can be composed with number 1 to N(each number can be and should be use
only once in this problem). So it's easy to see the second smallest
sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You
should tell me the Mth smallest sequence which is composed with number 1
to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
input contains several test cases. Each test case consists of two
numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume
that there is always a sequence satisfied the BEelzebub's demand. The
input is terminated by the end of file.
each test case, you only have to output the sequence satisfied the
BEelzebub's demand. When output a sequence, you should print a space
between two numbers, but do not output any spaces after the last number.
11 8
1 2 3 4 5 6 7 9 8 11 10
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
#define INF 0x7fffffff
int n,m,a[N];
int main()
{
while(cin>>n>>m){
for(int i=1;i<=n;i++)
a[i]=i; //初始化1~n的排列 while(--m) //注意从1开始
next_permutation(a+1,a+n+1); //1~n的排列的第m个 for(int i=1;i<n;i++) //注意格式
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
}
HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]的更多相关文章
- 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 全排列
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 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...
- HDU 1027 Ignatius and the Princess II 排列生成
解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...
- HDU 1027 Ignatius and the Princess II 选择序列题解
直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...
- 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 ...
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
随机推荐
- 怎么把myeclipse项目导入IDEA中
先把myeclipse下的项目拷贝到IDEA的部署目录中,把一些不用的配置文件删除,只留下一个干净的项目 打开IDEA,点击import Project,引入一个项目,选择IDEA部署目录下刚拷贝过去 ...
- data相关应用
文案参考:HTML5中的data-*属性和jQuery中的.data()方法使用 data属性选择器 $("li[data-id='1']")//选择li元素中data-id属性等 ...
- privoxy+ss5实现 HTTP 代理协议转socks5代理
一.系统准备资源 二.ss5安装部署 1.SOCK5代理服务器部署环境准备 IP:10.0.0.100 官网: http://ss5.sourceforge.net/ 下载 yum - ...
- unknow table alarmtemp error when drop database (mysql)
Q: unknow table alarmtemp error when drop database (mysql) D: alarmtemp is table in rtmd database. ...
- 纯js国际化(i18n)
i18n,是internationalization单词的简写,中间18个字符略去,简称i18n,意图就是实现国际化,方便产品在不同的场景下使用 目标:可以点击切换语言或者ChangeLanguage ...
- 图说不为人知的IT传奇故事-4-王安用一生来跟IBM抗衡
此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...
- 聊聊、Java 命令 第一篇
网上很多讲 Javac 和 Java 命令的,我觉得还是要自己写一写,做一个自己的总结,也方便以后查询. 开始之前先看看 help 命令,基本上任何一个软件都会提供这个命令. 没有什么比 -help ...
- BZOJ 1036: [ZJOI2008]树的统计Count(树链剖分)
树的统计CountDescription一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改 ...
- js确保正确this的几种写法
1.直接用bind调用 this.method.bind(this) 2.构造函数中用bind定义 class Foo{ constructor(){ this.method = this.metho ...
- docker (centOS 7) 使用笔记3 - docker swarm mode
1. 什么是docker swarm mode docker engine自带的 容器管理 工具.功能比较早的 docker swarm 更多,且集成在docker engine里. (docker ...