首先介绍一下next_permutation函数的用途!

按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止。

prev_permutation函数与之相反,是生成给定序列的上一个较小的排列。

代码如下

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {,,,};
do{
cout << a[] << " " << a[] << " " << a[]<<" "<<a[] << endl;
}
while (next_permutation(a,a+)); //system("pause");
return ;
}

3,6,4,2  下一个序列为 4 2 3 6

观察第一个序列可以发现pn中的6 4 2已经为减序,在这个子集中再也无法排出更大的序列了,因此必须移动3的位置且要找一个数来取代3的位置。在6 4 2中6和4都比3大,但6比3大的太多了,只能选4。将4和3的位置对调后形成排列4 6 3 2。注意,由于4和3大小的相邻关系,对调后产生的子集6 3 2仍保持逆序,即该子集最大的一种排列。

题目

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
 
就是利用这个神奇的函数即可完成
 
 
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,a[];
while(cin>>n>>m){
for(int i=;i<n;i++)a[i]=i+;
while(m-){
next_permutation(a,a+n);
m--;
}
for(int i=;i<n;i++){
if(i!=n-)cout<<a[i]<<" ";
else cout<<a[i]<<endl;
}
}
//system("pause");
return ;
}
 
 
 

A - Next_permutation的更多相关文章

  1. 关于全排列 next_permutation() 函数的用法

    这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...

  2. About next_permutation

    哈哈没错这个又是我们C++党的语言优势之一,用这个函数可以求当前排序的下一个排序,也就是说可以方便的求全排列,用这个函数需要用到algorithm这个头文件. 与这个函数相反的是prev_permut ...

  3. STL next_permutation和prev_permutation函数

    利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...

  4. 【STL】next_permutation的原理和使用

    1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...

  5. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

  6. next_permutation函数

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类 ...

  7. buaaoj230——next_permutation的应用

    题目地址 简单的全排列输出,借用stl中的next_permutation就非常简单了. 关于next_permutation:(备忘,来源网络) /*这是一个求一个排序的下一个排列的函数,可以遍历全 ...

  8. [算法]——全排列(Permutation)以及next_permutation

    排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...

  9. [LeetCode] next_permutation

    概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用 ...

  10. next_permutation 和 一个不成功的案例

    一个失败的案例:(POJ 1009) 题目描述 小翔同学的宿舍WIFI添加了密码,密码每天都会变更.而小翔每天都会给蹭网的同学们提供密码提示.现在请你根据密码提示,编写程序破译密码. 已知密码提示给出 ...

随机推荐

  1. No redirect found in host configuration file (C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet.config).

    Configuration Error Description: An error occurred during the processing of a configuration file req ...

  2. OC对象:封装、继承、多态的使用举例一

    // 该代码在网上找的视频中的例子,感觉很适合类和对象分不清楚的同学参考,仅供学习分享,谢谢 // 创建一个Pointtest类,用属性x.y表示点的坐标位置,求两点之间的距离,使用两种方法:类方法和 ...

  3. 进阶笔记(2)——JavaScript语言精碎

    正则       /     正则表达式      / ^   表示字符串开始 (?:...)   表示一个非捕获型分组(没多大意义) 后缀 ? 表示匹配 0 或 1次 ( ... )   表示捕获型 ...

  4. poj3159 Candies(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit ...

  5. POJ2533:Longest Ordered Subsequence(LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  6. mysql实现随机查询

    一.随机查询一条数据 方法一:SELECT * FROM `table` ORDER BY RAND() limit 1 评价:不建议使用,效率非常低,官方文档中进行说明:Order By和RAND( ...

  7. iis10,php 5.2 isapi 扩展

    1.添加isapi映射模块: 2.设置独立应用程序池,注意php版本,5.2,要设置程序池32位. 64位下配置IIS+PHP出现404.17错误的解决办法

  8. ORACLE的客户端如何连接到数据库

    如何连接oracle数据库及故障解决办法   如何配置才能使客户端连到数据库:     要使一个客户端机器能连接oracle数据库,需要在客户端机器上安装oracle的客户端软件,唯一的例外就是jav ...

  9. Linux中的ln

    在安装了wdcp或在正常使用wdcp后,如有意无意用使用了yum更新系统或安装软件,有时会直接更新安装了yum源里的apache,这时问题就来了打开所有的网站或页面,都是提示Apache欢迎页面 这个 ...

  10. 海量数据面试题----分而治之/hash映射 + hash统计 + 堆/快速/归并排序

    1.从set/map谈到hashtable/hash_map/hash_set 稍后本文第二部分中将多次提到hash_map/hash_set,下面稍稍介绍下这些容器,以作为基础准备.一般来说,STL ...