Advertisement
Derga

Untitled

Aug 26th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4. #include <algorithm>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. int getUnusedMin(int min, set <int>& already_used) {
  10.     while (already_used.find(min) != already_used.end()) {
  11.         min++;
  12.     }
  13.     return min;
  14. }
  15.  
  16. int getUnusedMax(int max, set <int>& already_used) {
  17.     while (already_used.find(max) != already_used.end()) {
  18.         max--;
  19.     }
  20.     return max;
  21. }
  22.  
  23. int main() {
  24.     int n;
  25.     cin >> n;
  26.  
  27.     vector <int> v(n);
  28.     for (auto& elem : v) {
  29.         cin >> elem;
  30.     }
  31.    
  32.     int min = 1;
  33.     int max = INT_MAX;
  34.     set <int> alredy_used;
  35.  
  36.     vector<int> result;
  37.     result.push_back(v[0]);
  38.     alredy_used.insert(v[0]);
  39.  
  40.     for (int i = 1; i < v.size(); ++i) {
  41.         min = getUnusedMin(min, alredy_used);
  42.         max = getUnusedMax(max, alredy_used);
  43.        
  44.         if (v[i - 1] == v[i]) {  
  45.             result.push_back(min);
  46.             result.push_back(max);
  47.             alredy_used.insert(min);
  48.             alredy_used.insert(max);
  49.             continue;
  50.         }
  51.  
  52.         if (v[i - 1] < v[i]) {
  53.             result.push_back((alredy_used.find(v[i]) != alredy_used.end() ? max : v[i]));
  54.             alredy_used.insert((alredy_used.find(v[i]) != alredy_used.end() ? max : v[i]));
  55.            
  56.             max = getUnusedMax(max, alredy_used);
  57.             result.push_back(max);
  58.             alredy_used.insert(max);
  59.             continue;
  60.         }
  61.  
  62.         result.push_back((alredy_used.find(v[i]) != alredy_used.end() ? min : v[i]));
  63.         alredy_used.insert((alredy_used.find(v[i]) != alredy_used.end() ? min : v[i]));
  64.  
  65.         min = getUnusedMin(min, alredy_used);
  66.         result.push_back(min);
  67.         alredy_used.insert(min);
  68.     }
  69.  
  70.     for (auto& elem : result) {
  71.         cout << elem << " ";
  72.     }
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement