CF7B.Memory Manager

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn bytes of the memory and return the allocated block's identifier xx ;
  • erase x — to erase the block with the identifier xx ;
  • 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 mm bytes, numbered for convenience from the first to the mm -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 nn 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.

内存资源管理器

题目描述

距离第一个国产操作系统 BerlOS 发布已经所剩无几了。它的一些组件尚未完成——内存管理器就是其中之一。根据开发人员的计划,在第一个版本中,内存管理器将非常简单且直线。

它将支持三种操作:
alloc n - 分配nn个字节的内存并返回分配块的标识符xx
erase x - 删除具有标识符xx的块;
defragment - 对空闲内存进行碎片整理,使所有块尽可能地靠近内存的开头并保留它们各自的顺序;

本题中的内存模型非常简单。它是一个mm字节的序列,为方便起见,编号从11开始直至mm

第一个操作alloc n将要分配的内存块的大小作为唯一的参数。在处理此操作时,将在内存中分配一个由nn个连续字节组成的空闲块。如果此类块的数量超过一个,则优先选择最接近内存开头(即第一个字节)的块。所有这些字节都被标记为不空闲,并且内存管理器返回一个 32 位整数数字标记,它是该块的标识符。如果无法分配此大小的空闲块,则该函数返回NULL

第二个操作删除x将某个块的标识符作为其参数。此操作释放系统内存,将该块的字节标记为可供进一步使用。如果该标识符不指向先前分配的块(该块尚未被删除),则该函数返回ILLEGAL_ERASE_ARGUMENT,即参数不合法。

最后一个操作碎片整理没有任何参数,只是使占用的内存部分更接近内存的开头,而不改变它们各自的顺序。

在当前的实现中,您将使用从11开始的连续整数作为标识符。每个成功的分配操作进程应返回以下数字。不成功的分配操作不会影响计数。

你的目标

您将编写内存管理器的实现。您应该输出每个分配命令的返回值。您还应该为所有失败的擦除命令输出ILLEGAL_ERASE_ARGUMENT

感谢Macw提供翻译

输入格式

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

输入包含t+1t+1行。
第一行输入两个整数ttmm,分别要操做的次数和计算机的内存的大小。
接下来的t行每一行输入一个指令集,三种指令的含义请参阅题目描述。

输出格式

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.

每一行一个整数或一个字符串。
每当用户使用alloc指令后,输出分配到的标识符xx。其中,若对于每一个alloc指令,如果无法分配此大小的空闲块,则该函数返回NULL
如果用户的操作不合法,输出ILLEGAL_ERASE_ARGUMENT

输入输出样例

  • 输入#1

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

    输出#1

    1
    2
    NULL
    3
    

说明/提示

请注意,本题的空间限制为64MB,是默认空间限制的二分之一

首页