#7 B. Memory Manager(细节+模拟)

题目链接:

点击打开链接

http://www.codeforces.com/contest/7/problem/B

Description

There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations:

alloc n — to allocate n bytes of the memory and return the allocated block's identifier x;
erase x — to erase the block with the identifier x;
defragment — to defragment the free memory, bringing all the blocks as close to the beginning of the memory as possible and preserving their respective order;
The memory model in this case is very simple. It is a sequence of m bytes, numbered for convenience from the first to the m-th.

The first operation alloc n takes as the only parameter the size of the memory block that is to be allocated. While processing this operation, a free block of n successive bytes is being allocated in the memory. If the amount of such blocks is more than one, the block closest to the beginning of the memory (i.e. to the first byte) is prefered. All these bytes are marked as not free, and the memory manager returns a 32-bit integer numerical token that is the identifier of this block. If it is impossible to allocate a free block of this size, the function returns NULL.

The second operation erase x takes as its parameter the identifier of some block. This operation frees the system memory, marking the bytes of this block as free for further use. In the case when this identifier does not point to the previously allocated block, which has not been erased yet, the function returns ILLEGAL_ERASE_ARGUMENT.

The last operation defragment does not have any arguments and simply brings the occupied memory sections closer to the beginning of the memory without changing their respective order.

In the current implementation you are to use successive integers, starting with 1, as identifiers. Each successful alloc operation procession should return following number. Unsuccessful alloc operations do not affect numeration.

You are to write the implementation of the memory manager. You should output the returned value for each alloc command. You should also output ILLEGAL_ERASE_ARGUMENT for all the failed erase commands.

Input

The first line of the input data contains two positive integers t and m (1 ≤ t ≤ 100;1 ≤ m ≤ 100), where t — the amount of operations given to the memory manager for processing, and m — the available memory size in bytes. Then there follow t lines where the operations themselves are given. The first operation is alloc n (1 ≤ n ≤ 100), where n is an integer. The second one is erase x, where x is an arbitrary 32-bit integer numerical token. The third operation is defragment.

Output

Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return integers, starting with 1, as the identifiers of the allocated blocks.

Sample Input

6 10
alloc 5
alloc 3
erase 1
alloc 6
defragment
alloc 6

Sample Output

1
2
NULL
3

题意:这个内存条一共有连续的 m 个块。给你n个操作。

alloc x,就是你要产生一个块,这个块的大小是x。前提是:存在一个连续区间可以放下这个块。那就放下去,而且要求尽量放在前面。如果不能放就输出NULL。不然就输出一个数,从1开始。
erase x,就是把编号为 x 的块擦除。非法擦除就输出ILLEGAL_ERASE_ARGUMENT
defragment,就是重新排序,把每个块都尽量放在前面,让后面的都是空的。

题解:

模拟题。注意一下细节,WA一两次就可以了....QAQ


AC代码:

#include<bits/stdc++.h>
using namespace std;
char s[100];
int n,m,ans;
int blocks=0;
int a[1010];
int alloc(int x)
{
	for(int i=1;i<=m;i++)
	{
		int j=i;
		int num=0;
		while(a[j]==0 && num !=x && j<=m)
		{
			num++;
			j++;
		}
		if(num!=x)
		{
			i=j;
		}
		else
		{
			blocks++;
			for(int k=i;k<j;k++)
			{
				a[k]=blocks;
			}
			return blocks;
		}

	}
	return 0;
}
int earse(int x)
{
	if(x<=0)
	{
		return -1;
	}
	int flag=0;
	for(int i=1;i<=m;i++)
	{
		while(a[i]==x)
		{
			a[i++]=0;
			flag=1;
		}
		
	}
	if(flag==1)
	{
		return -2;
	}
	return -1; //非法擦除 
}
int defragment()
{
	for(int i=1;i<=m;i++)
	{
		if(a[i]==0)
		{
			for(int j=i+1;j<=m;j++)
			{
				if(a[j])
				{
					swap(a[i],a[j]);break;
				}
			}
		}
	}
	return -5;
}
int main()
{
	int x;
	cin>>n>>m;
	memset(a,0,sizeof(a));
	while(n--)
	{
		scanf("%s",&s);
		if(s[0]=='a')
		{
			cin>>x;
			ans=alloc(x);
		}
		else if(s[0]=='e')
		{
			cin>>x;
			ans=earse(x);
		}
		else if(s[0]=='d')
		{
			ans=defragment();
		}
		
		if(ans==0){
			printf("NULL\n");
		}
		else if(ans==-1)
		{
			printf("ILLEGAL_ERASE_ARGUMENT\n");
		}
		else if(ans>=1)
		{
			printf("%d\n",ans);
		}
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值