Topic description
P1088 [NOIP2004 Popularization Group] Martian
next_permutation
Here are two functions for calculating the full permutation of a sequence:
- next_permutation(a,a+n)
- prev_permutation(start,end)
The parameter is the head and tail of the array. It is
intended to obtain the lower (upper) full order of the current full arrangement.
How to understand the upper and lower?
For example 1-3
, the full arrangement is
123
132
213
231
312
321
132 is the next full permutation of 123, otherwise it is the previous full permutation.
There is nothing to say about this question, and the use of this function is accurately examined.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int a[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int idx = m;
while (idx--) {
next_permutation(a, a+n);
}
for (int i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}