ACM_寻找第N小序列
寻找第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小序列的更多相关文章
- (寻找第K小的数&&寻找第K小的数的和)
这一篇博客以一些OJ上的题目为载体,讲一下寻找第K小的数的方法 方法一: 先将数据排列好,然后,然后return a[k]或者将前K个数加起来 方法二: 基于高速排序.如,一次高速排序将某一个数放到了 ...
- 寻找第K小元素
要在一个序列里找出第K小元素,可以用排序算法,然后再找.可以证明,排序算法的上界为O(nlogn). 在这里,给出两种可以在线性时间内找出第K小元素的方法. 方法1: (1) 选定一个比较小的阈值(如 ...
- 减治算法之寻找第K小元素问题
一.问题描写叙述 给定一个整数数列,寻找其按递增排序后的第k个位置上的元素. 二.问题分析 借助类似快排思想实现pation函数.再利用递归思想寻找k位置. 三.算法代码 public static ...
- BUPT复试专题—寻找第 K 小的数(2009)
题目描述 给你 n 个完全不相同整数(n<=300),每一个数都大于 0 并且小于 1000,请找出 第 k 小的数. 输入 输入包括两行,第一行用空格隔开的两个数 n 和 k;第二行有 n 个 ...
- BUPT复试专题—寻找变化前01序列(2016)
题目描述 给你一个01序列,HDLC协议处理的话,如果出现连续的5个1会补1个0.例如1111110,会变成11111010. 现在给你一个经过HDLC处理后的01序列,你需要找到HDLC处理之前的0 ...
- 算法导论 寻找第i小元素 9.2
PS1:如果单纯为做出这道题那么这个代价是O(nlgn),通过排序就可以了. 这里讨论的是O(n)的算法.那么来分析一下这个算法是如何做到O(n)的,算了不分析了,这个推到看起来太麻烦了.其实我想知道 ...
- 小明系列问题――小明序列(LIS)
小明系列问题――小明序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu----(4521)小明系列问题——小明序列
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- 小明系列问题——小明序列(Lis 相距大于d的单调上升子序列)
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
随机推荐
- LINUX-JPS工具
JPS工具 jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/u ...
- 18年多校-1002 Balanced Sequence
>>点击进入原题测试<< 思路:自己写没写出来,想不通该怎么排序好,看了杜神代码后补题A掉的.重新理解了一下优先队列中重载小于号的含义,这里记录一下这种排序方式. #inclu ...
- 【Codeforces 1129A】Toy Train
[链接] 我是链接,点我呀:) [题意] 火车从1,2,3...n->1的方式绕圈走.(即每次从i走到i+1) 有一些点有货物需要装载,但是每个点只能装上去一个货物. 每个货物都有目标点卸货点( ...
- hdu 2647拓扑排序 容器
#include<stdio.h> #include<queue> #include<vector> #include<iostream> using ...
- 后台获得集合,变成json字符串,放到EL表达式,js进行获取遍历
//把集合编程json字符串,放到el表达式 JSONArray array = new JSONArray(); JSONArray fromObject = array.fromObject(ar ...
- Remmarguts’ Date(poj 2449)
求第k短路的长度,如果没有,输出-1. /* k短路模板 注意当s=t时,k++. */ #include<iostream> #include<cstdio> #includ ...
- 控制公司(codevs 2051)
题目描述 Description 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.例如,福特公司拥有马自达公司12%的股票.据说,如果至少满足了以下三个条件之一,公司A就可以 ...
- 20181010关于pt-kill自动杀死运行超长的进程
转自: http://blog.chinaunix.net/uid-16844903-id-4442030.htmlhttp://blog.chinaunix.net/uid-31396856-id- ...
- Holedox Eating HDU4302 模拟
Problem Description Holedox is a small animal which can be considered as one point. It lives in a st ...
- Ubuntu 16.04安装PPA图形化管理工具Y PPA Manager
安装: sudo add-apt-repository ppa:webupd8team/y-ppa-manager sudo apt-get update sudo apt-get install y ...