您的位置:首页 > 编程语言 > Go语言

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

#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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法