Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <algorithm>
- void process(const std::vector<int> &v1, std::vector<int> &v2)
- {
- std::vector<int> v1_copy(v1);
- std::sort(v1_copy.begin(), v1_copy.end());
- auto valid_begin = v1_copy.begin();
- auto valid_end = std::unique(v1_copy.begin(), v1_copy.end());
- for (auto i = valid_begin; i != valid_end; i++) {
- if (*i < 0) {
- valid_begin = i + 1;
- } else if (size_t(*i) > v2.size()) {
- valid_end = i;
- break;
- }
- }
- if (valid_begin == valid_end) {
- return;
- }
- int counter = 0;
- auto j = valid_begin;
- for (auto i = v2.begin(); i != v2.end(); ++i) {
- if (j != valid_end && *j == std::distance(v2.begin(), i)) {
- ++j;
- ++counter;
- continue;
- }
- if (counter > 0) {
- std::swap(*(i), *(i - counter));
- }
- }
- v2.erase(v2.end() - counter, v2.end());
- }
Advertisement
Add Comment
Please, Sign In to add comment