SHARE
TWEET

Untitled

a guest Sep 21st, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<fstream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<string>
  5. #include<queue>
  6. #include<set>
  7. #include<map>
  8.  
  9. using namespace std;
  10.  
  11. ifstream input("INPUT.TXT");
  12. ofstream output("OUTPUT.TXT");
  13. vector<int> v;
  14.  
  15. void next() {
  16.     int pos = -1;
  17.     int valnow = v[v.size() - 1];
  18.     for (int i(v.size() - 2); i >= 0; i++) {
  19.         if (valnow > v[i]) {
  20.             pos = i;
  21.             break;
  22.         }
  23.         valnow = v[i];
  24.     }
  25.     if (pos = -1) {
  26.         reverse(v.begin(), v.end());
  27.     }
  28.     else {
  29.         int minmax = v[pos];
  30.         int minmaxpos = pos;
  31.         for (int i(pos + 1); i < v.size(); i++) {
  32.             if (v[i] > v[pos] && v[i] < minmax) {
  33.                 minmax = v[i];
  34.                 minmaxpos = i;
  35.             }
  36.         }
  37.         swap(v[pos], v[minmaxpos]);
  38.         reverse((v.begin() + pos + 1), v.end());
  39.     }
  40. }
  41.  
  42. void print(vector<int> a) {
  43.     for (int i(0); i < a.size(); i++) {
  44.         output << a[i] << ' ';
  45.     }
  46. }
  47.  
  48. int main() {
  49.     int n;
  50.     input >> n;
  51.     for (int i(0); i < n; i++) {
  52.         int a;
  53.         input >> a;
  54.         v.push_back(a);
  55.     }
  56.     next();
  57.     print(v);
  58.  
  59.     return 0;
  60. }
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