SHARE
TWEET

J.

a guest Nov 9th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. const double EPS = 1e-9;
  7. const int INF=1e9+7;
  8.  
  9. int n;
  10.  
  11. void perm(vector <int> & p) {
  12.     for (int i = n - 2; i >= 0; --i){
  13.     int id = i + 1;
  14.     if (p[i] < p[i + 1]){
  15.         for (int j = i + 1; j < n; ++j){
  16.             if (p[j] > p[i] && p[j] < p[id])
  17.                 id = j;
  18.         }
  19.         swap(p[i], p[id]);
  20.         reverse(p.begin() + i + 1, p.end());
  21.         return;
  22.         }
  23.     }
  24.   for (auto i: p) {
  25.       i = 0;
  26.   }
  27. }
  28.  
  29. void prev_perm(vector <int> & p) {
  30.     for (int i = n - 2; i >= 0; --i){
  31.     int id = i + 1;
  32.     if (p[i] > p[i + 1]){
  33.         for (int j = i + 1; j < n; ++j){
  34.             if (p[j] < p[i] && p[j] > p[id])
  35.                 id = j;
  36.         }
  37.         swap(p[i], p[id]);
  38.         reverse(p.begin() + i + 1, p.end());
  39.         return;
  40.         }
  41.     }
  42.   for (auto i: p) {
  43.       i = 0;
  44.   }
  45. }
  46.  
  47. int main() {
  48.     cin >> n;
  49.     vector<int> p1(n), p2(n);
  50.     for (int i = 0; i<n; ++i) {
  51.         cin >> p1[i];
  52.         p2[i] = p1[i];
  53.     }
  54.     perm(p1);
  55.     prev_perm(p2);
  56.     for (auto i: p1) {
  57.         cout << i << ' ';
  58.     }
  59.     cout << endl;
  60.     for (auto i: p2) {
  61.         cout << i << ' ';
  62.     }
  63.     cout << endl;
  64.     return 0;
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top