hihoCoder #1198 Memory Allocating Algorithm
2015-10-27 17:33
387 查看
Description
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.![](http://media.hihocoder.com//problem_images/20150802/14384837303016.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.
![](http://media.hihocoder.com//problem_images/20150802/14384837775587.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.
![](http://media.hihocoder.com//problem_images/20150802/14384838243133.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:
![](http://media.hihocoder.com//problem_images/20150802/14384838465945.png)
And we need to write the 9th data of size 4. The algorithm removes the 1st data:
![](http://media.hihocoder.com//problem_images/20150802/14384838771480.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:
![](http://media.hihocoder.com//problem_images/20150802/14384839029472.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.
Input
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
Output
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.Sample Hint
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
Sample Input
6 10 5 2 2 2 4 4
Sample Output
4 0 5 2 6 6
Solution:
#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); } }
相关文章推荐
- 2015 南阳 CCPC G.Ancient Go
- django cookies 和 session
- 在Go语言程序中使用gojson来解析JSON格式文件
- 038.category 分类/类别
- 039.category 对类进行模块化设计
- 040.category 调用私有方法
- 求最长回文子串的Manacher算法,time O(n)
- GO语言的IO方法实例小结
- windows下用eclipse+goclipse插件+gdb搭建go语言开发调试环境
- Go语言编程中对文件读写的基本方法整理
- 036.Goto 代替 Break 结束循环
- 制作自己的开发板logo
- Go语言编程中字符串切割方法小结
- Go语言中字符串的查找方法小结
- Django Admin:添加富文本编辑器kindeditor
- LSGO软件技术团队2015~2016学年第八周(1019~1025)总结
- LSGO软件技术团队内部技术交流【2015-2016(1)第七周】
- Django自定义上传目录
- 我在 Google 做技术经理的一天
- MFC嵌入google map