HDU 1027 Ignatius and the Princess II

HDU 1027 Ignatius and the Princess II

题目大意:

给你两个数N和M,然后求字典序第M小的1~N的排列

比如:3 1

那么1~3的第一小的字典序排列就是1 2 3

 

解题思路:

在我们做任何题目之前,都应该先计算一下最朴实无华的做法(暴力法)的时间复杂度

然后再根据我们计算出来的结果,去考虑应该怎么去优化来使时间复杂度达到题目时限能够接受的范围之内

 

看下这题,纯暴力法:1e3*1e4=1e7

你会发现纯暴力的时间复杂度并不是非常的夸张,直接暴力就能够解决本题

当然,要是不知道next_permutation()这个函数,还是不好解,但是知道了就是最基础的函数练习题

直接使用next_permutation()即可直接解决本题

 

代码如下:

 1 #include <bits/stdc++.h>
 2 #define mem(a) memset(a,0,sizeof(a))
 3 #define mem1(a) memset(a,-1,sizeof(a))
 4 #define forn(i,n) for(int i=0;i<n;++i)
 5 #define for1(i,n) for(int i=1;i<=n;++i)
 6 #define IO std::ios::sync_with_stdio(false); std::cin.tie(0)
 7 #define ll long long
 8 #define LL long long
 9 #define inf 0x3f3f3f3f
10 #define INF 0x3f3f3f3f3f3f3f3f
11 #define N 1000000
12 using namespace std;
13 const int maxn=1e3+50;
14 //ll gcd(ll a, ll b){return b ? gcd(b, a % b) : a;}
15 //ll lcm(ll a, ll b){return a * b / gcd(a, b);}
16 
17 int num[maxn];
18 
19 int main()
20 {
21     int n,m;
22     while(~scanf("%d %d",&n,&m))
23     {
24         for(int i=1;i<=n;++i)
25         {
26             num[i]=i;
27         }
28         int k=1;
29         do
30         {
31             next_permutation(num+1,num+n+1);
32             k++;
33         }while(k<m);
34         for(int i=1;i<=n;++i)
35         {
36             if(i!=1)
37                 printf(" %d",num[i]);
38             else
39                 printf("%d",num[i]);
40         }
41         printf("\n");
42     }
43     return 0;
44 }

 

posted @ 2020-10-19 18:21  小松QAQ  阅读(101)  评论(0编辑  收藏  举报