组合数学 + STL --- 利用STL生成全排列

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?
 

 

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的函数声明:

 

1
2
3
4
#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++程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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() ,函数声明如下:

1
2
#include <algorithm>
void sort( iterator start, iterator end );

sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。

1
2
3
4
5
6
7
#include <string>
iterator begin();
const_iterator begin() const;
 
#include <string>
iterator end();
const_iterator end() const;

string.begin()和string.end() 可以快速访问到字符串的首字符和尾字符。

发现以上函数的效率不是很高,可能是没开 g++ -O2 优化吧……
下面的程序换成puts来输出,比上面快多了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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\n",a[n-1]);
                break;
            }
        }
    }
    return 0;
}

  

posted @   北岛知寒  阅读(379)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩