Ignatius and the Princess II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4865    Accepted Submission(s): 2929
Problem Description
Now 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
The 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.
 
Output
For 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.
 
Sample Input
6 4
11 8
 
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
 题意: 给一个整数 N, 然后让求N的全排列中(按字母序排序), 第M小的序列。
分析: N最大可以到1000, 卧槽, 难道要求N! 吗?, 当然不是。 (除非脑子被ACMer碾压成了粉末!), 观察到M最大为 10000, 即:  M< 8!。 那么8的阶乘足矣。
然后就是裸裸的康托展开啦~~~~。(康托展开与康托逆展开
 
康托逆展开

例1 {1,2,3,4,5}的全排列,并且已经从小到大排序完毕
(1)找出第96个数
首先用96-1得到95
用95去除4! 得到3余23
有3个数比它小的数是4
所以第一位是4
用23去除3! 得到3余5
有3个数比它小的数是4但4已经在之前出现过了所以第二位是5(4在之前出现过,所以实际比5小的数是3个)
用5去除2!得到2余1
有2个数比它小的数是3,第三位是3
用1去除1!得到1余0
有1个数比它小的数是2,第二位是2
最后一个数只能是1
所以这个数是45321
(2)找出第16个数
首先用16-1得到15
用15去除4!得到0余15
用15去除3!得到2余3
用3去除2!得到1余1
用1去除1!得到1余0
有0个数比它小的数是1
有2个数比它小的数是3 但由于1已经在之前出现过了所以是4(因为1在之前出现过了所以实际比4小的数是2)
有1个数比它小的数是2 但由于1已经在之前出现过了所以是3(因为1在之前出现过了所以实际比3小的数是1)
有1个数比它小得数是2 但由于1,3,4已经在之前出现过了所以是5(因为1,3,4在之前出现过了所以实际比5小的数是1)
最后一个数只能是2
所以这个数是14352
 
上面就是康托逆展开的例子。 这道题也是这个例子的裸裸的模板! (注意第M大, 和第M小的区别!)
 
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; int a[] = {, , , , , , , , };
int vis[]; int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(vis, , sizeof(vis));
m--;
int temp = ;
while(temp<n)
{
if((n-temp)<=)
{
int k = m/a[n-temp];
int mod = m%a[n-temp];
int count = ;
for(int i=; i<=n; i++)
{
if(!vis[i]) ++count;
if((count-)==k)
{
printf("%d ", i);
vis[i] = ; break;
}
}
m = mod;
}
else
{
for(int i=; i<=n; i++)
{
if(!vis[i] == )
{
vis[i] = ;
printf("%d ", i); break;
}
}
}
++temp;
}
for(int i=; i<=n; i++)
if(!vis[i]) printf("%d\n", i);
}
return ;
}
 生成全排列,暴力求解!
#include <stdio.h>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std; const int maxn = ;
int a[maxn];
int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i=; i<=n; i++) a[i] = i;
int num = ;
while(num<m)
{
next_permutation(a+, a++n);
num++;
}
for(int i=; i<n; i++)
printf("%d ", a[i]);
printf("%d\n", a[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. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

    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[DFS/全排列函数next_permutation]

    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(正、逆康托)

    题意: 给N和M. 输出1,2,...,N的第M大全排列. 思路: 将M逆康托,求出a1,a2,...aN. 看代码. 代码: int const MAXM=10000; int fac[15]; i ...

  6. HDU 1027 Ignatius and the Princess II 选择序列题解

    直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...

  7. HDU 1027 - Ignatius and the Princess II

    第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...

  8. HDU 1027 Ignatius and the Princess II 排列生成

    解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...

  9. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

随机推荐

  1. href="#"会导致location.replace(location.href);脚本不工作

    我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...

  2. linux命令:du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  3. SpringMVC 接收ajax发送的数组对象

    本文粘贴自:http://my.oschina.net/jiefalcon/blog/384153?fromerr=24Lewn46 [转]SpringMVC @RequestBody接收Json对象 ...

  4. Prince2的七大原则(5)

    [Prince2科普]Prince2的七大原则(5) 按照惯例我们先来回顾一下,PRINCE2七大原则分别是指:持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第五个原则 ...

  5. 关于数据库表中的索引及索引列的CRUD

     -- 查询一个数据库表中的索引及索引列use [RuPengWangDB]GOSELECT  indexname = a.name , tablename = c. name , indexcolu ...

  6. Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏

    Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...

  7. wpf的研究和反思

    WPF的研究和反思 目前是否适合使用wpf      WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET F ...

  8. JAVA基础知识之JVM-——自定义类加载器

    JVM中除了根加载器之外其他加载器都是ClassLoader的子类实例, 可以通过扩展ClassLoader的子类,通过重写方法来实现自定义的类加载器. ClassLoader中有两个关键的方法如下, ...

  9. hiho(1081),SPFA最短路,(非主流写法)

    题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...

  10. apache-common pool的使用

    Apache commons-pool本质上是"对象池",即通过一定的规则来维护对象集合的容器;commos-pool在很多场景中,用来实现"连接池"/&quo ...