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 ...
随机推荐
- 笔记-网络-抓包-wireshark
笔记-网络-抓包-wireshark 1. 开始 环境:win8笔记本,无线网 1.1. 无线网卡设置 因为需抓捕无线网卡上的数据包,需要进行一项设置,如捕获有线网卡,无需设置. 打开 ...
- 第5模块闯关CSS练习题
1.列举你知道的css选择器? 说道css选择器,大家都知道有许多种,但是真要你去掰着手指头数一数的话,你可能需要数几分钟.其实这么多选择器,完全可以分为两类: 标签选择器(*是特殊情况),可但标签, ...
- Falsk
flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...
- python学习之dictionary函数的用法
编写下面这段代码运行出现了报错.#!/usr/bin/env python2.7#-*-coding:utf-8 -*- d=['T']a=raw_input('请输入a的值')if a in d : ...
- php 实现栈与队列
<?php class queueOp{ /* * 队尾入队 * Return:处理之后队列的元素个数 */ public function tailEnquque($arr,$val){ re ...
- iBATIS 框架主要的类层次结构
iBATIS 框架主要的类层次结构 总体来说 iBATIS 的系统结构还是比较简单的,它主要完成两件事情: 根据 JDBC 规范建立与数据库的连接: 通过反射打通 Java 对象与数据库参数交互之间相 ...
- 【转】Unity摄像机的使用二:摄像机的切换
http://blog.csdn.net/liujunjie612/article/details/45847877 我们接着上一篇博文继续研究摄像机,这篇我们说下Unity中摄像机的切换,说到切换, ...
- P4302 [SCOI2003]字符串折叠
题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS…S(X个S). 如果A = A’, B = ...
- JAVA File方法文本复制读写-解决中文乱码
import java.io.*; public class TextFile { public static void main(String[] args) throws Exception { ...
- 妹子(girls)
妹子(girls) 题目描述 万人迷皮皮轩收到了很多妹子的礼物,由于皮皮轩觉得每个妹子都不错,所以将她们礼物的包装盒都好好保存,但长此以往皮皮轩的房间里都堆不下了,所以只能考虑将一些包装盒放进其他包装 ...