Advertisement
mfgnik

Untitled

Nov 19th, 2020
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. std::vector<int> NextPermutation(std::vector<int> permutation, size_t len) {
  5.     size_t k = len - 2;
  6.     while (permutation[k] > permutation[k + 1]) {
  7.         --k;
  8.     }
  9.     size_t t = k + 1;
  10.     while (t < len - 1 && permutation[t + 1] > permutation[k]) {
  11.         ++t;
  12.     }
  13.     int temp = permutation[k];
  14.     permutation[k] = permutation[t];
  15.     permutation[t] = temp;
  16.     while (k + 1 < len - 1) {
  17.         temp = permutation[k + 1];
  18.         permutation[k + 1] = permutation[len - 1];
  19.         permutation[len - 1] = temp;
  20.         ++k;
  21.         --len;
  22.     }
  23.     return permutation;
  24. }
  25. bool comp(int a, int b) {
  26.     return a >= b;
  27. }
  28. int main() {
  29.     int n;
  30.     std::cin >> n;
  31.     std::vector<int> current(n);
  32.     for (int i = 0; i < n; ++i) {
  33.         std::cin >> current[i];
  34.     }
  35.     auto current_copy = current;
  36.     if (std::prev_permutation(current.begin(), current.end())) {
  37.         for (auto i: current) {
  38.             std::cout << i << " ";
  39.         }
  40.     } else {
  41.         for (auto i : current) {
  42.             std::cout << 0 << " ";
  43.         }
  44.     }
  45.     std::cout << "\n";
  46.     if (std::next_permutation(current_copy.begin(), current_copy.end())) {
  47.         for (auto i: current_copy) {
  48.             std::cout << i << " ";
  49.         }
  50.     } else {
  51.         for (auto i : current) {
  52.             std::cout << 0 << " ";
  53.         }
  54.     }
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement