hiho108周即题库1198Memory Allocating Algorithm
2016-07-24 13:33
459 查看
描述
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.
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.
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.
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:
And we need to write the 9th data of size 4. The algorithm removes the 1st data:
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:
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
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.
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.
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.
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:
And we need to write the 9th data of size 4. The algorithm removes the 1st data:
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:
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); } }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法