寻找第N小序列

Time Limit: 2000/1000ms (Java/Others)

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
解题思路:万能STL中的next_permutation(a,a+n)可以找到第m-1个全排列。注:如果m>(n!-1),next_permutation(a,a+n)就会继续从有序序列开始再进行全排列。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
int n,m,a[];
int main(){
while(cin>>n>>m){
for(int i=;i<n;++i)a[i]=i+;
for(int i=;i<m;++i)next_permutation(a,a+n);//第m-1次的全排列
for(int i=;i<n;++i)cout<<a[i]<<(i==n-?'\n':' ');
}
return ;
}

ACM_寻找第N小序列的更多相关文章

  1. (寻找第K小的数&amp;&amp;寻找第K小的数的和)

    这一篇博客以一些OJ上的题目为载体,讲一下寻找第K小的数的方法 方法一: 先将数据排列好,然后,然后return a[k]或者将前K个数加起来 方法二: 基于高速排序.如,一次高速排序将某一个数放到了 ...

  2. 寻找第K小元素

    要在一个序列里找出第K小元素,可以用排序算法,然后再找.可以证明,排序算法的上界为O(nlogn). 在这里,给出两种可以在线性时间内找出第K小元素的方法. 方法1: (1) 选定一个比较小的阈值(如 ...

  3. 减治算法之寻找第K小元素问题

    一.问题描写叙述 给定一个整数数列,寻找其按递增排序后的第k个位置上的元素. 二.问题分析 借助类似快排思想实现pation函数.再利用递归思想寻找k位置. 三.算法代码 public static ...

  4. BUPT复试专题—寻找第 K 小的数(2009)

    题目描述 给你 n 个完全不相同整数(n<=300),每一个数都大于 0 并且小于 1000,请找出 第 k 小的数. 输入 输入包括两行,第一行用空格隔开的两个数 n 和 k;第二行有 n 个 ...

  5. BUPT复试专题—寻找变化前01序列(2016)

    题目描述 给你一个01序列,HDLC协议处理的话,如果出现连续的5个1会补1个0.例如1111110,会变成11111010. 现在给你一个经过HDLC处理后的01序列,你需要找到HDLC处理之前的0 ...

  6. 算法导论 寻找第i小元素 9.2

    PS1:如果单纯为做出这道题那么这个代价是O(nlgn),通过排序就可以了. 这里讨论的是O(n)的算法.那么来分析一下这个算法是如何做到O(n)的,算了不分析了,这个推到看起来太麻烦了.其实我想知道 ...

  7. 小明系列问题――小明序列(LIS)

    小明系列问题――小明序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  8. hdu----(4521)小明系列问题——小明序列

    小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  9. 小明系列问题——小明序列(Lis 相距大于d的单调上升子序列)

    小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

随机推荐

  1. 在 Oculus和 Gear VR上开发跨平台的 VR应用

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51433994 作者:car ...

  2. noip模拟赛 毁灭

    题目描述 YJC决定对入侵C国的W国军队发动毁灭性打击.将C国看成一个平面直角坐标系,W国一共有n^2个人进入了C国境内,在每一个(x,y)(1≤x,y≤n)上都有恰好一个W国人.YJC决定使用m颗核 ...

  3. poj 3923 模拟

    /* 1.判断是否是一个完整边框 2.判断是否长度和宽度小于3 3.判断是否有内部覆盖的现象 */ #include<stdio.h> #define N 110 #define inf ...

  4. 1874 Bellman-ford算法 队列优化过的 用于稀疏图,有负权的图

    #include<stdio.h> #include<algorithm> #include<iostream> #include<queue> usi ...

  5. [NOIP2004]FBI树

    题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三 ...

  6. JSOI建筑抢修 (贪心+堆)

    先按照T2从小到大排序,然后进行贪心. 第i个任务能完成的条件是,sigma(T1[j])+T1[i]<=T2[i] ( j 为之前所选的任务) 如果这个任务不能完成,若max(T1[j]) & ...

  7. CodeForces 370C

    这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...

  8. Memory+SLES 11/12 OS Tuning & Optimization

    https://www.suse.com/documentation/sles11/book_sle_tuning/data/sec_util_memory.html SLES 11/12 OS Tu ...

  9. 虚拟化(二):虚拟化及vmware workstation产品使用

    虚拟化(一):虚拟化及vmware产品介绍 vmware workstation的最新版本号是10.0.2. 相信大家也都使用过,当中的简单的虚拟机的创建.删除等,都非常easy.这里就不再具体说明了 ...

  10. impex 语法

    impex 语法 2016-01-14 16:23 588人阅读 评论(0) 收藏 举报  分类: hybris(8)  脱离java Model单纯的去看impex文件的代码是不能很好理解impex ...