Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <queue>
- #include <vector>
- using namespace std;
- struct SegLengthPos {
- int length;
- int beg_pos;
- int operation_idx;
- };
- bool operator< (const SegLengthPos& lhs, const SegLengthPos& rhs) {
- if (lhs.length != rhs.length) return lhs.length > rhs.length;
- return lhs.beg_pos < rhs.beg_pos;
- }
- int main() {
- list<SegLengthPos> l;
- auto comparator = [](const list<SegLengthPos>::iterator& lhs, const list<SegLengthPos>::iterator& rhs) {
- return *lhs < *rhs;
- };
- priority_queue <list<SegLengthPos>::iterator,
- vector<list<SegLengthPos>::iterator>, decltype(comparator)> pq(comparator);
- int memory_cells_count, requests_count;
- cin >> memory_cells_count >> requests_count;
- vector<list<SegLengthPos>::iterator> history(1 + requests_count);
- l.push_back({ memory_cells_count, 1 });
- pq.push(l.begin());
- for (int i = 1; i <= requests_count; ++i) {
- int operation;
- cin >> operation;
- if (operation > 0) {
- if (pq.top()->length < operation) {
- history[i] = l.end();
- cout << -1;
- } else {
- auto seg_data = *pq.top();
- auto it = l.erase(pq.top());
- pq.pop();
- if (it != l.end()) prev(it);
- auto it1 = l.insert(it, { seg_data.length - operation, seg_data.beg_pos + operation, i});
- pq.push(it1);
- history[i] = it1;
- cout << seg_data.beg_pos << '\n';
- }
- continue;
- }
- if (history[operation] == l.end()) continue;
- auto it = history[operation];
- auto [len, pos, op_odx] = *history[operation];
- history[operation] = l.end();
- auto l_it = it;
- auto r_it = it;
- if (l_it != l.begin()) prev(l_it);
- if (r_it != l.end()) next(r_it);
- if (r_it != l.end() && it->beg_pos + it->length == r_it->beg_pos) {
- it->length += r_it->length;
- history[r_it->operation_idx] = l.end();
- l.erase(r_it);
- }
- if (l_it != it && l_it->beg_pos + l_it->length == it->beg_pos) {
- l_it->length += it->length;
- history[it->operation_idx] = l.end();
- l.erase(it);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement