Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- std::vector<int> NextPermutation(std::vector<int> permutation, size_t len) {
- size_t k = len - 2;
- while (permutation[k] > permutation[k + 1]) {
- --k;
- }
- size_t t = k + 1;
- while (t < len - 1 && permutation[t + 1] > permutation[k]) {
- ++t;
- }
- int temp = permutation[k];
- permutation[k] = permutation[t];
- permutation[t] = temp;
- while (k + 1 < len - 1) {
- temp = permutation[k + 1];
- permutation[k + 1] = permutation[len - 1];
- permutation[len - 1] = temp;
- ++k;
- --len;
- }
- return permutation;
- }
- bool comp(int a, int b) {
- return a >= b;
- }
- int main() {
- int n;
- std::cin >> n;
- std::vector<int> current(n);
- for (int i = 0; i < n; ++i) {
- std::cin >> current[i];
- }
- auto current_copy = current;
- if (std::prev_permutation(current.begin(), current.end())) {
- for (auto i: current) {
- std::cout << i << " ";
- }
- } else {
- for (auto i : current) {
- std::cout << 0 << " ";
- }
- }
- std::cout << "\n";
- if (std::next_permutation(current_copy.begin(), current_copy.end())) {
- for (auto i: current_copy) {
- std::cout << i << " ";
- }
- } else {
- for (auto i : current) {
- std::cout << 0 << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement