比赛 20250904开学热身赛 评测结果 WWWWW
题目名称 内存分配 最终得分 0
用户昵称 对立猫猫对立 运行时间 0.738 s
代码语言 C++ 内存使用 3.83 MiB
提交时间 2025-09-04 21:58:53
显示代码纯文本
/*没有看懂这个题解和我的代码有什么区别
#include <bits/stdc++.h>
#define INF 0x3fffffff
using namespace std;
int n, w = INF, cnt = 0;
struct data {
	int t, m, p, s;
	bool operator <(const data& x)const {
		return s < x.s;
	}
} x;
vector<data> p;
queue<data> q;
bool work_in(int t) {
	if (p.empty() || p[0].s >= x.m) {
		x.s = 0;
		x.t = t;
		p.push_back(x);
		sort(p.begin(), p.end());
		return 1;
	}
	for (int i = 1; i < p.size(); i++)
		if (p[i].s - (p[i - 1].s + p[i - 1].m) >= x.m) {
			x.s = p[i - 1].s + p[i - 1].m;
			x.t = t;
			p.push_back(x);
			sort(p.begin(), p.end());
			return 1;
		}
	int sz = p.size();
	if (n - (p[sz - 1].s + p[sz - 1].m) >= x.m) {
		x.s = p[sz - 1].s + p[sz - 1].m;
		x.t = t;
		p.push_back(x);
		sort(p.begin(), p.end());
		return 1;
	}
	return 0;
}
void work_out() {
	int nw = INF;
	for (int i = 0; i < p.size(); i++)
		if (p[i].t + p[i].p == w) p.erase(p.begin() + i--);
		else nw = min(nw, p[i].t + p[i].p);
	while (q.size()) {
		x = q.front();
		if (work_in(w)) {
			nw = min(nw, q.front().t + q.front().p);
			q.pop();
			cnt++;
		} else break;
	}
	w = nw;
}
void work(int t, int m, int p) {
	while (t >= w) work_out();
	x.t = t;
	x.m = m;
	x.p = p;
	if (work_in(t)) w = min(w, t + p);
	else q.push(x);
}
int main() {
	scanf("%d", &n);
	int t0, m0, p0;
	while (scanf("%d%d%d", &t0, &m0, &p0) == 3 && !(t0 == 0 && m0 == 0 && p0 == 0))
		work(t0, m0, p0);
	while (q.size()) work_out();
	int ans = w;
	for (int i = 0; i < p.size(); i++)
		ans = max(ans, p[i].t + p[i].p); //统计答案
	printf("%d\n%d\n", ans, cnt);
	return 0;
}
*/
/*ODT*/
#include <bits/stdc++.h>
using namespace std;

int n, cnt_wait = 0;
struct mem {
	int t, m, p;
	bool operator<(const mem &a) const {
		return t + p > a.t + a.p;    // 用于优先队列(最小堆)
	}
};
queue<mem> wait; // 等待队列
priority_queue<mem> running; // 运行中的进程(按结束时间排序的最小堆)

// 有序动态树(ODT)相关代码保持不变
struct Node_t {
	int l, r;
	mutable int v;
	Node_t(const int &il, const int &ir, const int &iv) : l(il), r(ir), v(iv) {}
	bool operator<(const Node_t &o) const {
		return l < o.l;
	}
};
set<Node_t> odt;

auto split(int x) {
	auto it = odt.lower_bound(Node_t(x, 0, 0));
	if (it != odt.end() && it->l == x) return it;
	--it;
	int l = it->l, r = it->r, v = it->v;
	odt.erase(it);
	odt.insert(Node_t(l, x - 1, v));
	return odt.insert(Node_t(x, r, v)).first;
}

void assign(int l, int r, int v) {
	auto itr = split(r + 1), itl = split(l);
	odt.erase(itl, itr);
	odt.insert(Node_t(l, r, v));
}

bool perform(int l, int r, int size) {
	auto itr = split(r + 1), itl = split(l);
	for (; itl != itr; ++itl) {
		if (itl->v == 0 && itl->r - itl->l + 1 >= size) return true;
	}
	return false;
}

bool now_mem(int size) {
	return perform(1, n, size);
}

void mem_allocate(mem tmp) {
	auto it = odt.begin();
	while (it != odt.end()) {
		if (it->v == 0 && it->r - it->l + 1 >= tmp.m) {
			assign(it->l, it->l + tmp.m - 1, 1);
			break;
		}
		++it;
	}
}

void mem_release(mem tmp) {
	auto it = odt.begin();
	while (it != odt.end()) {
		if (it->v == 1 && it->r - it->l + 1 == tmp.m) {
			assign(it->l, it->r, 0);
			break;
		}
		++it;
	}
}

int main() {
	freopen("memory.in", "r", stdin);
	freopen("memory.out", "w", stdout);
	cin >> n;
	odt.insert(Node_t(1, n, 0));
	int current_time = 0;
	vector<mem> processes;
	while (true) {
		int t, m, p;
		cin >> t >> m >> p;
		if (t == 0 && m == 0 && p == 0) break;
		processes.push_back({t, m, p});
	}
	int index = 0;
	while (index < processes.size() || !running.empty() || !wait.empty()) {
// 处理所有在current_time之前结束的进程
		while (!running.empty() && running.top().t + running.top().p <= current_time) {
			mem tmp = running.top();
			running.pop();
			mem_release(tmp);
// 尝试调度等待队列
			while (!wait.empty() && now_mem(wait.front().m)) {
				mem w = wait.front();
				wait.pop();
				mem_allocate(w);
				running.push(w);
			}
		}

		if (index < processes.size()) {
			mem cur = processes[index];
// 如果当前时间小于下一个进程的到达时间,则推进时间到到达时间
			if (current_time < cur.t) {
				current_time = cur.t;
				continue;
			}
			index++;
			if (now_mem(cur.m)) {
				mem_allocate(cur);
				running.push(cur);
			} else {
				wait.push(cur);
				cnt_wait++;
			}
		} else {
// 如果没有新进程,则推进时间到下一个结束的进程
			if (!running.empty()) {
				mem next = running.top();
				current_time = next.t + next.p;
			} else {
				break;
			}
		}
	}

// 处理所有剩余运行中的进程
	while (!running.empty()) {
		mem tmp = running.top();
		running.pop();
		current_time = max(current_time, tmp.t + tmp.p);
		mem_release(tmp);
		while (!wait.empty() && now_mem(wait.front().m)) {
			mem w = wait.front();
			wait.pop();
			mem_allocate(w);
			running.push(w);
		}
	}

	cout << current_time << endl << cnt_wait << endl;
	return 0;
}