vlad7576

up03-3 (delete two vectors)

May 17th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <vector>
  2. #include <algorithm>
  3.  
  4. void process(const std::vector<int> &v1, std::vector<int> &v2)
  5. {
  6.     std::vector<int> v1_copy(v1);
  7.  
  8.     std::sort(v1_copy.begin(), v1_copy.end());
  9.  
  10.     auto valid_begin = v1_copy.begin();
  11.     auto valid_end = std::unique(v1_copy.begin(), v1_copy.end());
  12.  
  13.  
  14.     for (auto i = valid_begin; i != valid_end; i++) {
  15.         if (*i < 0) {
  16.             valid_begin = i + 1;
  17.         } else if (size_t(*i) > v2.size()) {
  18.             valid_end = i;
  19.             break;
  20.         }
  21.     }
  22.     if (valid_begin == valid_end) {
  23.         return;
  24.     }
  25.     int counter = 0;
  26.     auto j = valid_begin;
  27.  
  28.     for (auto i = v2.begin(); i != v2.end(); ++i) {
  29.         if (j != valid_end && *j == std::distance(v2.begin(), i)) {
  30.             ++j;
  31.             ++counter;
  32.             continue;
  33.         }
  34.  
  35.         if (counter > 0) {
  36.             std::swap(*(i), *(i - counter));
  37.         }
  38.     }
  39.     v2.erase(v2.end() - counter, v2.end());
  40. }
Advertisement
Add Comment
Please, Sign In to add comment