Всем привет участники CodeForces! У меня тут вопрос по поводу итераторов. В недавном турнире Codeforces Round #696 (Div. 2) в задаче С
[https://codeforces.com/contest/1474/problem/C] разбирая решебник я столкнулся с проблемой Segmentation fault: 11 в функции ~~~~~ vector check(int n, vector a, int x) { multiset s; for (auto e : a) s.insert(e); vector res; for (int i = 0; i < n; i++) { auto it1 = s.end(); it1--; int y = x — *it1; s.erase(it1); auto it2 = s.find(y); if (it2 == s.end()) return {}; res.push_back(x — y); res.push_back(y); x = max(x — y, y); s.erase(it2); } return res; } ~~~~~
Решение задачи автора: https://codeforces.com/contest/1474/submission/104840760
Я переделал код чут подсебя: ~~~~~ vector check(int a[], int n, int x) { multiset s;
for(int i=0; i < 2*n; ++i){ s.insert(a[i]); } vector<int> ans; for(int i=0; i < n; ++i){ auto max_pos = s.end(); //выбираем максимальный элемент max_pos--; int b = x - *max_pos; s.erase(max_pos); // удаляем максимальный элемент if(s.find(b) == s.end()){ return {}; } x = max(*max_pos, b); ans.push_back(*max_pos); ans.push_back(b); s.erase(s.find(b)); } return ans;
} ~~~~~
Теперь вопрос, почему если я опускаю строку где комментария "// удаляем максимальный элемент" ниже if то дает Segmentation fault: 11, если так взглянуть то это же ассоциативное? @IgorI можете тоже посмотреть? Спасибо!