IamCXK @ 2023-02-16 15:01:17
// LUOGU_RID: 102240633
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long top, m;
long long block[105]; //内存条
long long block_1[105]; //内存条副本,用来defragment()
long long alloc(long long n)
{
bool flag = false;
long long start;
for(long long i=1, j; i<=m; i++)
if(block[i] == 0)
{
j = i;
while(block[++j] == 0 && j < m);
//cout<<"[j = "<<j<<" i = "<<i<<']'<<endl;
if(j - i >= n)//内存够
{
flag = true;
start = i;//空闲空间初始位置
break;
}
i = j - 1;
}
//cout<<"flag = "<<flag<<endl;
if(flag)//有空间存储
{
for(long long i=start; i<start+n; i++)
block[i] = top;
top++;
return top-1;
}
else
return -1;
}
bool erase(long long x)
{
bool flag = false;
for(long long i=1; i<=m; i++)
if(block[i] == x)
{
block[i] = 0;
flag = true;
break;
}
return flag;
}
void defragment()
{
for(long long i=1, j=1; i<=m; i++)
if(block[i] != 0)
block_1[j++] = block[i];//block数组缩进后存放到副本
for(long long i=1; i<=m; i++)
block[i] = block_1[i];//把副本复制回内存条
}
void print()
{
for(long long i=1; i<=100; i++)
{
cout<<block[i]<<' ';
if(i % 10 == 0)
cout<<endl;
}
}
int main()
{
long long t;
top++;
cin>>t>>m;
while(t--)
{
string operate;
cin>>operate;
if(operate[0] == 'd')
{
defragment();
//print();
}
else
{
long long n;
scanf("%lld", &n);
if(operate == "alloc")
{
long long flag = alloc(n);
//print();
if(flag == -1)
cout<<"NULL"<<endl;
else
cout<<flag<<endl;
}
else
{
bool flag = erase(n);
//print();
if(!flag)
cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
}
}
}
return 0;
}
print();是测试用的,没实际意义