比赛 20250904开学热身赛 评测结果 AAAAA
题目名称 内存分配 最终得分 100
用户昵称 xuyuqing 运行时间 0.153 s
代码语言 C++ 内存使用 3.71 MiB
提交时间 2025-09-04 20:30:25
显示代码纯文本

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int n;
set<pair<int, int> > me;
queue<pair<int, int> > q;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int res;
int cc;

bool give (int t, int m, int p) {
	for (auto it = me.begin(); it != me.end(); it++) {
		auto nxt = it;
        nxt++;

        if (nxt != me.end() && nxt->first - it->second - 1 >= m) {
            me.emplace(it->second + 1, it->second + m);
            pq.emplace(t + p, it->second + 1);
            return true;
        }
	}
    return false;
}

void clean (int t) {
    while (!pq.empty() && pq.top().first <= t) {
        auto time = pq.top().first;
        res = time;
        
        while (!pq.empty() && pq.top().first == time) {
            auto tmem = pq.top();
            pq.pop();
            me.erase(me.lower_bound({tmem.second, 0}));
        }
        while (!q.empty()) {
            auto wait = q.front();
            if (give (time, wait.first, wait.second)) {
                q.pop();
            }
            else {
                break;
            }
        }
    }
}

int main () {
	
	freopen ("memory.in", "r", stdin);
	freopen ("memory.out", "w", stdout);
	
	cin >> n;
	
	me.insert({-1, -1});
	me.insert({n, n});

    int t, m, p;
    while (true) {
        cin >> t >> m >> p;
        if (t == 0 && m == 0 && p == 0) {
            break;
        }
        clean (t);
        if (!give (t, m, p)) {
            cc++;
            q.emplace(m, p);
        }
    }

    clean (1e9 + 10);

    cout << res << endl << cc << endl;
	
	return 0;
}