hiho108周即题库1198Memory Allocating Algorithm

2024-05-25 01:18

本文主要是介绍hiho108周即题库1198Memory Allocating Algorithm,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

描述
Little Hi is studying on memory allocating algorithms this summer. He starts his experiments with a very simple algorithm. By this algorithm memory is considered as a sequence of M consecutive storage units, numbered from 0 to M-1.

memory_1.png

Whenever a piece of data is written to the memory the algorithm finds from 0 to M-1 the first segment of consecutive empty units which is large enough and save the data there. For example if the data size is 2 after saving it the memory look as below. Units are marked as 1 because they contain the 1st data we write.

memory_2.png

If we continue to write two pieces of data of size 3 and 2 the memory looks like below. Units 2-4 contain the 2nd data and Units 5 and 6 contain the 3rd data.

memory_3.png

If there is not enough consecutive empty units for the coming data the algorithm will keep removing the earliest data until the coming data can be saved. Assume the memory if full after we write the 8th data:

memory_4.png

And we need to write the 9th data of size 4. The algorithm removes the 1st data:

memory_5.png

There is still not enough consecutive empty units so the 2nd data is also removed. Then the 9th data is saved at Units 0-3:

memory_6.png

Remember if there are multiple possible segments to save the coming data the algorithm always choose the segment which is started at the unit of the smallest number.

After writing N data of different sizes Little Hi wants to know what the memory looks like.

输入
Line 1: N and M, the number of data and the size of the memory.

Line 2: N integers, K1, K2 …, KN. Ki is the size of the ith data.

For 60% of the data 1≤N≤200,10≤M≤100,1≤Ki≤5

For 100% of the data 1≤N≤2,000,10≤M≤109,1≤Ki≤M

输出
For each data which is still in the memory at last output one line of two integers id and s: the number of the data and its starting position in the memory. Output them in increasing order of id.

样例提示
The memory looks after saving each data:

1 1 1 1 1 0 0 0 0 0

1 1 1 1 1 2 2 0 0 0

1 1 1 1 1 2 2 3 3 0

4 4 0 0 0 2 2 3 3 0

4 4 5 5 5 5 0 3 3 0

4 4 5 5 5 5 6 6 6 6

样例输入
6 10
5 2 2 2 4 4
样例输出
4 0
5 2
6 6

#include <cstdio>
#include <deque>
#include <algorithm>    
using namespace std;struct chunk {int v, s, e;
};int main() {int n, m;scanf("%d%d", &n, &m);deque<chunk> que;int c;bool f = true;for (int k = 1; k <= n; ++k) {if(f) scanf("%d", &c);if (que.empty()) {que.push_back({ k, 0, c - 1 });}else {bool flag = false;int minK = n + 1;int idx = -1;for (int i = 0; i < que.size(); ++i) {if (minK > que[i].v) {minK = que[i].v;idx = i;}if (i == 0) {if (c   - 1 < que[i].s) {que.push_front({ k, 0, c - 1 });flag = true;break;}}if (i == que.size() - 1) {if (que[i].e + c < m) {que.push_back({ k, que[i].e+1, que[i].e + c });flag = true;break;}}else {if (que[i + 1].s - que[i].e - 1 >= c) {que.insert(que.begin() + i + 1, { k, que[i].e + 1, que[i].e + c });flag = true;break;}}}if (!flag) {que.erase(que.begin() + idx);k--;f = false;}else {f = true;}}}sort(que.begin(), que.end(), [](const chunk &ch1, const chunk &ch2) {return ch1.v < ch2.v;});for (int k = 0; k < que.size(); ++k) {printf("%d %d\n", que[k].v, que[k].s);}
}

这篇关于hiho108周即题库1198Memory Allocating Algorithm的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1000068

相关文章

2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题

题库来源:安全生产模拟考试一点通公众号小程序 2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题是由安全生产模拟考试一点通提供,流动式起重机司机证模拟考试题库是根据流动式起重机司机最新版教材,流动式起重机司机大纲整理而成(含2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题参考答案和部分工种参考解析),掌握本资料和学校方法,考试容易。流动式起重机司机考试技

华为 HCIP-Datacom H12-821 题库 (13)

有需要题库的可以看主页置顶 1.可以携带外部路由的 tag 标签信息的是以下哪一类 LSA? A、4 类 LSA B、5 类 LSA  C、3 类 LSA  D、2 类 LSA 答案:B 解析: 暂无解析 2..两台路由器直连,并设定网络类型为 p2p 建立OSPF 邻居。那么两台路由器传输 OSPF 报文的目的 IP 地址是以下哪一项? A、使用组播地址 224.0.0.6 B

2024年【防爆电气】考试题库及防爆电气复审模拟考试

题库来源:安全生产模拟考试一点通公众号小程序 防爆电气考试题库根据新防爆电气考试大纲要求,安全生产模拟考试一点通将防爆电气模拟考试试题进行汇编,组成一套防爆电气全真模拟考试试题,学员可通过防爆电气复审模拟考试全真模拟,进行防爆电气自测。 1、【单选题】Exib级的本安设备可连接到()危险场所。(  C  ) A、0、1区 B、0、2区 C、1、2区 2、【单选题】充油型电气设备

【tensorflow 使用错误】tensorflow2.0 过程中出现 Error : Failed to get convolution algorithm

如果在使用 tensorflow 过程中出现 Error : Failed to get convolution algorithm ,这是因为显卡内存被耗尽了。 解决办法: 在代码的开头加入如下两句,动态分配显存 physical_device = tf.config.experimental.list_physical_devices("GPU")tf.config.experiment

近2千消防题库工程师题库ACCESS\EXCEL数据库

这次获得的一批行业题库,数据库表结构都是一样的,有《近万条电气工程师考试题库》、《1万2千多条电工考试题库》、《5千多道安全生产证考试题库》以及今天的消防工程师题库。 大类记录汇总情况:高级#注#册#消防工程师(790)、一级#注#册#消防工程师(1183)。题型汇总情况为:单选题(1498)、多选题(475)。 截图下方有显示“共有记录数”,截图包含了表的所有字段列。该数据提供ACC

纪念一下自己的Coursera Princeton Algorithm的课程第一个assignment

今天终于完成了第一个Union-Find的assignment,之前觉得特别的难,可是最后自己也搞定了。而且是100%满分。 自己后来plot了一下自己的分数,也许这就是学习曲线吧。刚开始不会,到后来中期显著提高,但是要到100%,那就要经历更多的波折,甚至是下降都有可能。最后才能达到100%满分。 我觉得最有用的还是下面这段源代码: /*************************

中国化学工程第七建设校招|EAS测评题库智联招聘攻略考什么

中国化学工程第七建设有限公司(简称“七化建”)是一家隶属于中国化学工程集团有限公司的全资子公司,属于央企。公司业务领域广泛,包括石油化工、房屋建筑、水利水电、市政公用、道路桥梁等EPC总承包、技术开发、实业投资、贸易等。公司在国内外都有项目,并且有着良好的企业文化和发展前景。 对于校招方面,七化建对应聘者有一系列的要求,包括学历、专业、英语水平、专业资格证书等。例如,翻译类岗位要求取得专业四级及

宝藏!《联盟自控强化班洗髓题库》(青龙篇) 1-9章:甄选部分

本文内容,全部选自自动化考研联盟的:初试《自控强化班洗髓题库》(青龙篇)。 目录 Part1:资料封面&目录 Part2:资料各个章节具体内容 第1章 自动控制的一般概念 第2章 控制系统的数学模型 第3章 线性系统的时域分析 第4章 线性系统的根轨迹法 第5章 线性系统的频域分析法 第6章 线性系统的校正方法 第7章 线性离散系统的分析与校正 第8章 非线性控制系统分析

[Algorithm][综合训练][栈和排序][加减]详细讲解

目录 1.栈和排序1.题目链接2.算法原理详解 && 代码实现 2.加减1.题目链接2.算法原理详解 && 代码实现 1.栈和排序 1.题目链接 栈和排序 2.算法原理详解 && 代码实现 解法:栈 + 贪心 -> 每次尽可能先让当前需要的最大值弹出去vector<int> solve(vector<int>& a) {int n = a.size();vect

[Algorithm][综合训练][四个选项][接雨水]详细讲解

目录 1.四个选项1.题目链接2.算法原理详解 && 代码实现 2.接雨水1.题目链接2.算法原理详解 && 代码实现 1.四个选项 1.题目链接 四个选项 2.算法原理详解 && 代码实现 解法:DFS(暴搜) + 剪枝 + Hash 剪枝: 填某个数的时候,要看看还有没有剩余次数填某个数的时候,符不符合若干题的选项必须相同 #include <iostr