-
Notifications
You must be signed in to change notification settings - Fork 0
/
Next_permutationImplementation.cpp
72 lines (65 loc) · 1.04 KB
/
Next_permutationImplementation.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define N 1000006
#define M 30
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
int a[N], ac[N];
void init(int n){
for(int i=1; i<=n; i++){
a[i] = rand()%N;
ac[i] = a[i];
}
}
void print(int x, int n){
if(x == 0){
for(int i=1; i<=n; i++){
cout<<a[i]<<" ";
}
}
else{
for(int i=1; i<=n; i++){
cout<<ac[i]<<" ";
}
}
cout<<"\n";
}
void NP(int n){
int i, j;
i = n;
while(i > 1 && a[i-1] >= a[i]) i--;
if(i == 1) return;
j = n;
while(a[j] <= a[i-1]) j--;
swap(a[j], a[i-1]);
j = n;
while(i < j){
swap(a[i], a[j]);
i++; j--;
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
srand(time(0));
int i, j, n;
cin>>n;
init(n);
/*for(i=1; i<=n; i++){
cin>>a[i];
}*/
print(0, n);
print(1, n);
NP(n);
print(0, n);
next_permutation(ac+1, ac+n+1);
print(1, n);
return 0;
}