Complete_Reality @ 2023-06-23 20:25:11
#include<bits/stdc++.h>
using namespace std;
int q , n , x , len;
bool flag;
string s;
struct node{
int st , ed , used;
friend bool operator<(node x , node y) {
if (x.used == y.used) return x.st < y.st;
return x.used > y.used;
}
}a[2000005];
int main() {
scanf("%d %d", &q , &n);
while(q --) {
cin >> s;
if (s == "alloc") {
scanf("%d", &x);
sort(a + 1 , a + len + 1);
if (flag) {
int L = a[1].ed - a[1].st + 1;
a[1].st = 1;
a[1].ed = L;
for (int i = 2 ; i <= len ; i ++) {
if (!a[i].used) break;
int L = a[i].ed - a[i].st + 1;
a[i].st = a[i - 1].ed + 1;
a[i].ed = a[i].st + L - 1;
}
flag = 0;
}
int last = 0 , tmp = -1;
for (int i = 1 ; i <= len ; i ++) {
if (a[i].used) last = i;
if (!a[i + 1].used) break;
if (a[i + 1].st - a[i].ed - 1 >= x) {
tmp = i;
break;
}
}
if (a[1].st - 1 >= x) tmp = 0;
if (n - a[last].ed >= x && tmp == -1) tmp = last;
if (tmp == -1) {
puts("NULL");
continue;
}
a[++ len].st = a[tmp].ed + 1;
a[len].used = 1;
a[len].ed = a[len].st + x - 1;
printf("%d\n", len);
} else if (s == "erase") {
scanf("%d", &x);
if (!a[x].used) {
puts("ILLEGAL_ERASE_ARGUMENT");
continue;
}
a[x].used = 0;
} else flag = 1;
}
return 0;
}
基本思路:
对于每次插入操作,先对当前存储器进行排序,找到能够满足条件的空位并插入,特判第
1 个和最后1 个
对于defragment 操作,留到插入操作的时候再处理
对于erase 操作,直接标记已被删除
by Complete_Reality @ 2023-06-23 20:27:49
有没有哪位过路的好心人帮帮