codeforces 7B
1 second
64 megabytes
standard input
standard output
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.
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 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.
6 10
alloc 5
alloc 3
erase 1
alloc 6
defragment
alloc 6
1
2
NULL
3
一道纯模拟题,模拟内存分配过程。(os的首次适应算法
a:从前往后寻找一个满足大小的内存空间,放进去并给个编号(编号一直累加
e:清空给定编号的空间
d:把所有内存块移到最前面,空闲块都存在最后面了。
这题有两个大坑:1.内存块移到前面时不改变顺序,这个顺序不是编号的顺序,而是他们内存块的位置顺序。2.不合理编号的处理要细心点。
附ac代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <iostream>
5 #include <algorithm>
6 using namespace std;
7 string op;
8 int mem[111];
9 int idm[111];
10 struct nod
11 {
12 int st;
13 int ed;
14 int id;
15 }nd[111];
16 bool cmp(nod a,nod b)
17 {
18 if(a.st<b.st) return 1;
19 return 0;
20 }
21 int main()
22 {
23 ios::sync_with_stdio(false);
24 cin.tie(0);
25 cout.tie(0);
26 int t,m;
27 cin>>t>>m;
28 for(int i=1;i<=111;++i)
29 nd[i].id=i;
30
31 int u,id=1;
32 for(int i=1;i<=t;++i)
33 {
34
35 cin>>op;
36 if(op[0]=='a')
37 {
38 cin>>u;
39
40
41 int j=1,sum=0;
42 // cout<<"m:"<<mem[1]<<endl;
43 for(j=1;j<=m;++j)
44 {
45 if(mem[j]==0)
46 {
47 sum++;
48 }
49 else
50 {
51 sum=0;
52 }
53 if(sum==u)
54 {
55 int k=j;
56 while(sum!=0)
57 {
58 mem[k-sum+1]=1;
59 sum--;
60 }
61 break;
62 }
63 }
64 if(j==m+1) cout<<"NULL"<<endl;
65 else
66 {
67 cout<<id<<endl;
68 idm[id]=1;
69 nd[id].st=j-u+1;
70
71 nd[id++].ed=j;
72 // cout<<nd[id-1].st<<nd[id-1].ed<<endl;
73 }
74 }
75 else if(op[0]=='e')
76 {
77 cin>>u;
78 if(u<0 || u>id || idm[u]==0)
79 {
80 cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
81 }
82 else
83 {
84 // cout<<nd[id-1].st<<nd[id-1].ed<<endl;
85 idm[u]=0;
86 for(int k=1;k<id;++k)
87 {
88
89 if(nd[k].id==u)
90 {
91
92 for(int j=nd[k].st;j<=nd[k].ed;++j)
93 {
94
95 mem[j]=0;
96 }
97 }
98 }
99
100 }
101 }
102 else
103 {
104 int sum=0,x=1;
105 for(int k=1;k<=m;++k)
106 {
107 if(mem[k]==1)
108 sum++;
109 }
110 memset(mem,0,sizeof(mem));
111 for(int k=1;k<=sum;++k)
112 mem[k]=1;
113 sort(nd+1,nd+id,cmp);
114 for(int k=1;k<id;++k)
115 {
116
117 if(idm[nd[k].id]==1)
118 {
119 // cout<<nd[k].id<<endl;
120 int len=nd[k].ed-nd[k].st+1;
121 nd[k].st=x;
122 nd[k].ed=x+len-1;
123 x=nd[k].ed+1;
124 // cout<<nd[k].st<<nd[k].ed<<endl;
125 }
126 }
127 }
128 }
129 return 0;
130 }
codeforces 7B的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- Vue MVVM模型原理
最近反思了下自己,觉得自己很急躁,学技术总是觉得能用就行了,其实这样很不好,总是这样,就永远只能当用轮子的人.好了,废话不多说,转入正题: 要理解MVVM的原理,首先要理解它是什么,怎么运作起来的: ...
- 使用bapi创建PO遇到问题(BAPI_PO_CREATE1
今天用 BAPI_PO_CREATE1创建po. 注意事项: vendor 供应商号:长度必须和系统一致,10位.如 2000025要写成 0002000025传递给参数. POITEM 中的 PO_ ...
- 图解 ECDHE 密钥交换算法
HTTPS 常用的密钥交换算法有两种,分别是 RSA 和 ECDHE 算法. 其中,RSA 是比较传统的密钥交换算法,它不具备前向安全的性质,因此现在很少服务器使用的.而 ECDHE 算法具有前向安全 ...
- 三十三:WEB漏洞-逻辑越权之水平垂直越权
水平和垂直越权 水平越权:可以获得同级别用户权限 垂直权限:享受高几个层次的用户权限 解释,原理,检测,利用,防御 通过更换的某个ID之类的身份标识,从而使得A账号获取(修改,删除)B账号的数据,通过 ...
- Nifi组件脚本开发—ExecuteScript 使用指南(一)
Part 1 - 介绍 NiFi API 和 FlowFiles ExecuteScript 是一个万能的处理器,允许用户使用编程语言定义自己的数据处理功能, 在每一次 ExecuteScript p ...
- 一文告诉你Java日期时间API到底有多烂
前言 你好,我是A哥(YourBatman). 好看的代码,千篇一律!难看的代码,卧槽卧槽~其实没有什么代码是"史上最烂"的,要有也只有"史上更烂". 日期是商 ...
- Android LocationManagerService启动(一)
Location服务是系统中很重要的一个服务,几乎当前所有的App都会用到这个服务. 首先看代码在Android源码的位置 Android API frameworks/base/location L ...
- loj10172
涂抹果酱 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×M 的矩形,它被划分成 N×M 个边长为 1×1 的小正方形区域(可以把蛋糕当成 NNN 行 MMM ...
- C# 实现语音聊天
一.语音聊天说专业点就是即时语音,是一种基于网络的快速传递语音信息的技术,普遍应用于各类社交软件中,优势主要有以下几点: (1)时效性:视频直播会因为带宽问题有时出现延迟高的问题,而语音直播相对来说会 ...
- SpringMVC听课笔记(七:Restful CRUD)
这章貌似没有什么可讲的,可以看GitHub工程代码: https://github.com/heyboom/SpringMVC_Rest_CRUD