洛谷P1088 火星人【STL】【思维】

题目https://www.luogu.org/problemnew/show/P1088

题意:

给定一个n个数的排列,要求得到这之后的第m个排列。

思路:

next_permutation的简单应用。

题意本身是说找到m加上当前值之后在火星人的表示法里的数。

但是本身加减顺序是可换的,题意可以变换成当前的值之后的第m个

所以当前的排列之前的根本不需要管,只用从当前开始,跑m次next_permutation就可以了。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<map>
 4 #include<set>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<vector>
 9 #include<cmath> 
10 #include<queue>
11 
12 #define inf 0x7f7f7f7f
13 using namespace std;
14 typedef long long LL;
15 typedef pair<int, int> pr;
16 
17 int n, m;
18 const int maxn = 10005;
19 int num[maxn];
20 
21 int main()
22 {
23     scanf("%d%d", &n, &m);
24     for(int i = 0; i < n; i++){
25         scanf("%d", &num[i]);
26     }
27     while(m--){
28         next_permutation(num, num + n);
29     }
30     printf("%d", num[0]);
31     for(int i = 1; i < n; i++){
32         printf(" %d", num[i]);
33     }
34     printf("\n");
35     
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/wyboooo/p/10380754.html