CF7B.Memory Manager

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:64MB

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 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.

距离首款国产操作系统 BerlOS 的发布已所剩时间无几。其部分组件尚未完成——内存管理器便是其中之一。根据开发人员的计划,首版内存管理器将极为简洁直接,仅支持以下三种操作:

  • alloc n —— 分配 n 字节内存,并返回所分配内存块的标识符 x;
  • erase x —— 删除标识符为 x 的内存块;
  • defragment —— 对空闲内存进行整理(碎片整理),将所有已分配的内存块尽可能地向内存起始位置移动,同时保持它们原有的相对顺序;

本题中的内存模型极为简单:它是一段长度为 m 字节的连续内存,为便于描述,字节编号从第 1 字节至第 m 字节。

第一个操作 alloc n 以待分配内存块的大小 n 作为唯一参数。执行该操作时,需在内存中寻找一个大小为 n 的连续空闲块。若存在多个满足条件的空闲块,则优先选择最靠近内存起始位置(即最靠近第 1 字节)的那一块。该块内所有字节均被标记为“已占用”,内存管理器返回一个 32 位整数形式的标识符,用以唯一标识该内存块。若无法找到大小为 n 的连续空闲块,则函数返回 NULL。

第二个操作 erase x 以某内存块的标识符 x 作为参数。该操作将释放系统内存,把该内存块所占的所有字节重新标记为“空闲”,以供后续使用。若该标识符 x 并未指向一个此前已成功分配、且尚未被擦除的内存块,则函数返回 ILLEGAL_ERASE_ARGUMENT。

最后一个操作 defragment 不接受任何参数,其功能仅为将所有已占用的内存区域向内存起始位置紧凑排列,同时严格保持它们之间的原有相对顺序。

在当前实现中,你须使用从 1 开始的连续整数作为内存块标识符。每次成功执行 alloc 操作后,应返回下一个整数作为新块的标识符。失败的 alloc 操作不影响标识符的递增序列。

你需要实现该内存管理器。对于每个 alloc 命令,输出其返回值;对于所有失败的 erase 命令,输出 ILLEGAL_ERASE_ARGUMENT。

输入格式

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.

输入数据的第一行包含两个正整数 tt 和 mm(1 ≤ t ≤ 1001 \le t \le 100;1 ≤ m ≤ 1001 \le m \le 100),其中 tt 表示内存管理器需要处理的操作数量,mm 表示可用内存大小(单位:字节)。随后是 tt 行,每行给出一个操作。第一种操作为 alloc n(1 ≤ n ≤ 1001 \le n \le 100),其中 nn 是一个整数;第二种操作为 erase x,其中 xx 是任意一个 32 位整数数值标记;第三种操作为 defragment。

输出格式

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 操作处理的结果,或在 erase 操作处理失败时输出 ILLEGAL_ERASE_ARGUMENT。各行输出顺序应与操作处理顺序一致。alloc 操作成功处理时应返回整数作为所分配内存块的标识符,起始值为 1。

输入输出样例

  • 输入#1

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

    输出#1

    1
    2
    NULL
    3

输入解题思路,AI测评打分。不知道怎么写?

首页