import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Mem {
int handle;
int start;
int count;
public Mem(int handle, int start, int count) {
this.handle = handle;
this.start = start;
this.count = count;
}
}
public class Main {
public static void fill(boolean[] memory, int start, int count, boolean flag) {
for (int i = start; i < start + count; i++) {
memory[i] = flag;
}
}
public static void def(boolean[] memory, Map<Integer, Mem> allocatedMap) {
int k = 0;
for (Map.Entry<Integer, Mem> entry : allocatedMap.entrySet()) {
Mem currMem = entry.getValue();
currMem.start = k;
for (int i = 0; i < currMem.count; i++) {
memory[k++] = true;
}
}
for (int i = k; i < memory.length; i++) {
memory[i] = false;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int instructionCount = in.nextInt();
int memSize = in.nextInt();
Map<Integer, Mem> allocatedMap = new HashMap<>();
boolean[] memory = new boolean[memSize];
int newMemCount = 1;
in.nextLine();
for (int i = 0; i < instructionCount; i++) {
String instruction = in.nextLine();
//分配内存
if (instruction.startsWith("new")) {
String[] temp = instruction.split(" ");
int allocSize = Integer.parseInt(temp[1]);
int allocSizeCount = allocSize;
int pos = 0;
while (pos < memSize && memory[pos]) {
pos++;
}
int start = pos;
while (pos < memSize && !memory[pos]) {
pos++;
allocSizeCount--;
if (allocSizeCount == 0) {
break;
}
}
if (allocSizeCount == 0) {
System.out.println(newMemCount);
fill(memory, start, allocSize, true);
Mem mem = new Mem(newMemCount, start, allocSize);
allocatedMap.put(newMemCount++, mem);
} else {
System.out.println("NULL");
}
} else if (instruction.startsWith("del")) {
//删除内存
String[] temp = instruction.split(" ");
boolean found = false;
if (temp.length == 2) {
int handleNum = Integer.parseInt(temp[1]);
Mem removedMem = allocatedMap.remove(handleNum);
if (removedMem != null) {
found = true;
fill(memory, removedMem.start, removedMem.count, false);
}
}
if (temp.length != 2 || !found) {
System.out.println("ILLEGAL_OPERATION");
}
} else if (instruction.startsWith("def")) {
//整理内存
def(memory, allocatedMap);
}
}
}
}
}
input demo:
6 10 //6条指令,总内存为10M
new 5 //分配5M内存
new 3 //分配3M内存
del 1 //清除第1次分配的内存
new 6 //分配6M内存
def //整理内存,清除碎片
new 6 //分配6M内存
output demo:
1 //第1次分配内存句柄1
2 //第2次分配内存句柄2
NULL //空间不足,无法分配内存
3 //第3次分配内存句柄3