HDU 1027 Ignatius and the Princess II(求第m个全排列)

时间:2021-06-11 08:57:11

传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1027

Ignatius and the Princess II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10388    Accepted Submission(s): 5978

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
 
Author
Ignatius.L
 
分析:
题目意思:
1~n的全排列
求第m个(按照字典序排序)
复杂点的方法就是直接按照求全排列的方法
求出第m个就可以
但是我用的是另外一种方法
调用next_permutation函数:调用依次此函数就是把这个序列变成下一个序列
求到第m个输出就好
code:
 
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
int a[n];
int k=;
for(int i=;i<n;i++)
{
a[i]=i+;
}
while(next_permutation(a,a+n))//调用依次此函数就是把这个序列变成下一个序列
{
k++;
if(k==m)
break;
}
printf("%d",a[]);
for(int i=;i<n;i++)
{
printf(" %d",a[i]);
}
printf("\n");
}
return ;
}

HDU 1027 Ignatius and the Princess II(求第m个全排列)的更多相关文章

  1. HDU 1027 Ignatius and the Princess II(康托逆展开)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  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. HDU 1027 Ignatius and the Princess II&lbrack;DFS&sol;全排列函数next&lowbar;permutation&rsqb;

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

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

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

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

  6. HDU 1027 - Ignatius and the Princess II

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

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

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

  8. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next&lowbar;permutation函数)

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

  9. poj 1027 Ignatius and the Princess II全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

随机推荐

  1. ubuntu Chromium 安装 pepperflashplugin

    sudo apt-get update sudo apt-get install chromium-browser sudo apt-get install pepperflashplugin-non ...

  2. VFP MSSOAPTOOKIT 使用SOAP Headers

    .NET 有如下使用了自定义扩展HEADER来做验证  server.asmx代码 using System;using System.Collections.Generic;using System ...

  3. &lbrack;转&rsqb;很详细的devexpress应用案例

    很详细的devexpress应用案例,留着以后参考. 注:转载自http://***/zh-CN/App/Feature.aspx?AppId=50021 UPMS(User Permissions ...

  4. RobotFramework——介绍篇

    1.简介Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行.主要用于轮次很多的验 ...

  5. JAVA面向对象程序设计——实验报告

  6. 【转载】UML类图知识整理

    原文:UML类图知识整理 UML类图 UML,进阶必备专业技能,看不懂UML就会看不懂那些优秀的资料. 这里简单整理 类之间的关系 泛化关系(generalization) 泛化(generalize ...

  7. 19&rpar; Java并发

    >synchronized synchronized 关键字,它包括两种用法:synchronized 方法和 synchronized 块.  1>synchronized 方法:通过在 ...

  8. C&plus;&plus;中虚函数的作用是什么?它应该怎么用呢?&lpar;转&rpar;

    虚函数联系到多态,多态联系到继承.所以本文中都是在继承层次上做文章.没了继承,什么都没得谈. 下面是对C++的虚函数这玩意儿的理解. 一, 什么是虚函数(如果不知道虚函数为何物,但有急切的想知道,那你 ...

  9. JQuery easyui (2)Droppable(放置)组件

    所谓放置,就是将一个物体放入一个物体内,当然对于easyui来说触发各种效果是必不可少的,同时这个组件也不会依赖于其他组件. Droppable的加载方式 1,class  加载   一直不太喜欢cl ...

  10. vmware提示:此虚拟机似乎正在使用中,取得该虚拟机的所有权失败错误

    用vm的时候,没有挂起和关闭虚拟机,直接关实体机.然后不幸的就异常了. 启动提示:此虚拟机似乎正在使用中.如果此虚拟机已在使用中,请按"取消"按钮,以免损坏它.如果此虚拟机未使用, ...