Ignatius and the Princess II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4730 Accepted Submission(s): 2840
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?
"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
Mean:
给你一个n和m,让你输出从1~n这n个数全排列的第m个序列。
analyse:
如果我们用暴力的话,这题肯定超时,还好STL中自带了个这样的函数,可以直接调用。
下面来讲解一下 next_permutation 函数的运用:
在C++ Reference中查看了一下next_permutation的函数声明:
#include <algorithm> bool next_permutation( iterator start, iterator end ); The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.
从说明中可以看到 next_permutation 的返回值是布尔类型。按照提示写了一个标准C++程序:
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string str; //// 也可以是其他容器 cin >> str; sort(str.begin(), str.end()); cout << str << endl; while (next_permutation(str.begin(), str.end())) { cout << str << endl; } return 0; }
其中还用到了 sort 函数和 string.begin()、string.end() ,函数声明如下:
#include <algorithm> void sort( iterator start, iterator end );
sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。
#include <string> iterator begin(); const_iterator begin() const; #include <string> iterator end(); const_iterator end() const;
string.begin()和string.end() 可以快速访问到字符串的首字符和尾字符。
发现以上函数的效率不是很高,可能是没开 g++ -O2 优化吧……
下面的程序换成puts来输出,比上面快多了。
#include <cstdio> #include <algorithm> #include <cstring> #define MAX 100 using namespace std; int main() { int length; char str[MAX]; gets(str); length = strlen(str); sort(str, str + length); puts(str); while (next_permutation(str, str + length)) { puts(str); } return 0; }
Time complexity:O(n)?
Source code:
// Memory Time // 1347K 0MS // by : Snarl_jsb // 2014-09-15-22.17 #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<map> #include<string> #include<climits> #include<cmath> #define N 1000010 #define LL long long using namespace std; int main() { // freopen("C:\Users\ASUS\Desktop\cin.cpp","r",stdin); // freopen("C:\Users\ASUS\Desktop\cout.cpp","w",stdout); int n,m; vector<int> a; while(cin>>n>>m) { a.clear(); for(int i=1;i<=n;++i) { a.push_back(i); } int cnt=1; while(next_permutation(a.begin(),a.end())) { cnt++; if(cnt==m) { for(int i=0;i<n-1;i++) { printf("%d ",a[i]); } printf("%d ",a[n-1]); break; } } } return 0; }