Codeforces Beta Round #7 B. Memory Manager 模拟题
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
Hint
题意
给你alloc x,就是你要产生一个块,这个块的大小是x,如果存在一个区间可以放下这个块的话
那就放下去,而且尽量放在前面
erase x,就是把编号为x的全部擦掉
defragment,就是重新排序,把每个块都尽量放在前面。
题解:
模拟题……
理清思路,然后慢慢就可以怼过去了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
int pos[maxn];
int num;
string s;
int d;
int n,m;
void add()
{
cin>>d;
int flag=-1;
for(int j=1;j<=m;j++)
{
int p=0;
for(int k=j;k<=j+d-1&&k<=m;k++)
if(pos[k]==0)p++;
if(p==d)
{
flag=j;
break;
}
}
if(flag==-1)
{
printf("NULL\n");
return;
}
else
{
num++;
for(int i=flag;i<=flag+d-1;i++)
pos[i]=num;
printf("%d\n",num);
}
}
void del()
{
int d;cin>>d;
if(d<=0){printf("ILLEGAL_ERASE_ARGUMENT\n");return;}
int flag = -1;
for(int i=1;i<=m;i++)
if(pos[i]==d)
flag=1,pos[i]=0;
if(flag==-1)printf("ILLEGAL_ERASE_ARGUMENT\n");
}
void getsort()
{
int i=1,j=1;
for(i=1;i<=m;i++)if(pos[i])pos[j++]=pos[i];
for(;j<=m;j++)pos[j]=0;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
cin>>s;
if(s=="alloc")add();
if(s=="erase")del();
if(s=="defragment")getsort();
}
}
Codeforces Beta Round #7 B. Memory Manager 模拟题的更多相关文章
- Codeforces Beta Round #5 B. Center Alignment 模拟题
B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...
- Codeforces Beta Round #51 A. Flea travel 水题
A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
- Codeforces Beta Round #72 (Div. 2 Only)
Codeforces Beta Round #72 (Div. 2 Only) http://codeforces.com/contest/84 A #include<bits/stdc++.h ...
- Codeforces Beta Round #67 (Div. 2)
Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #65 (Div. 2)
Codeforces Beta Round #65 (Div. 2) http://codeforces.com/contest/71 A #include<bits/stdc++.h> ...
随机推荐
- 背包DP FOJ 2214
题目:http://acm.fzu.edu.cn/problem.php?pid=2214 (http://www.fjutacm.com/Problem.jsp?pid=2053) 这题看起来是一题 ...
- sar命令使用【转】
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...
- 【BubbleCup X】D. Exploration plan
这个题首先一眼能看出二分答案…… 毕竟连可爱的边界都给你了. 下面就是怎么check 首先预处理跑一遍floyed,预处理出最短路. 用网络流判断能否达到即可. #include<bits/st ...
- openjudge-NOI 2.6-2728 摘花生
题目链接:http://noi.openjudge.cn/ch0206/2728/ 题解: 某一个点只能从其左边或者上边走过来 f[i][j]存储(i,j)这个点上的结果,即f[i][j]=max(f ...
- Django 1.10文档中文版Part2
目录 2.5 第一个Django app,Part 3:视图和模板 2.5.1 概览 2.5.2 编写更多的视图 2.5.3 编写能实际干点活的视图 2.5.4 404错误 2.5.5 使用模板系统 ...
- nginx解析带中文的url重定向之后404问题
首先,有这样一个需求:一个系统的图片存储是放在upyun上的,现在有个客户需要本地化部署,所以需要将图片进行本地存储.为了兼容,图片存储的路径格式保持和在upyun上的存储路径一致.在upyun上的存 ...
- POJ 3253 Fence Repair(哈夫曼编码)
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...
- 四、ansible主机组定义
1.打开hosts文件 vim /etc/ansible/hosts 2.定义一个主机组 [web-server] 192.168.1.1 3.定义多个组(继承) [web:children] web ...
- luogu P1549 棋盘问题(2) 题解
luogu P1549 棋盘问题(2) 题解 题目描述 在\(N * N\)的棋盘上\((1≤N≤10)\),填入\(1,2,-,N^2\)共\(N^2\)个数,使得任意两个相邻的数之和为素数. 例如 ...
- Mysql修改语句的运行流程
执行修改语句前要先连接数据库,这是连接器的工作. 接下来,分析器会通过词法和语法解析知道这是一条更新语句.优化器决定要使用 ID 这个索引.然后,执行器负责具体执行,找到这一行,然后更新. Mysql ...