hihoCoder #1198 Memory Allocating Algorithm

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.

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.

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:

 1 #include <cstdio>
 2 #include <deque>
 3 #include <algorithm>
 4 using namespace std;
 5
 6
 7 struct chunk {
 8     int v, s, e;
 9 };
10
11 int main() {
12     int n, m;
13     scanf("%d%d", &n, &m);
14     deque<chunk> que;
15     int c;
16     bool f = true;
17     for (int k = 1; k <= n; ++k) {
18         if(f) scanf("%d", &c);
19         if (que.empty()) {
20             que.push_back({ k, 0, c - 1 });
21         }
22         else {
23             bool flag = false;
24             int minK = n + 1;
25             int idx = -1;
26             for (int i = 0; i < que.size(); ++i) {
27                 if (minK > que[i].v) {
28                     minK = que[i].v;
29                     idx = i;
30                 }
31                 if (i == 0) {
32                     if (c - 1 < que[i].s) {
33                         que.push_front({ k, 0, c - 1 });
34                         flag = true;
35                         break;
36                     }
37                 }
38                 if (i == que.size() - 1) {
39                     if (que[i].e + c < m) {
40                         que.push_back({ k, que[i].e+1, que[i].e + c });
41                         flag = true;
42                         break;
43                     }
44
45                 }
46
47                 else {
48                     if (que[i + 1].s - que[i].e - 1 >= c) {
49                         que.insert(que.begin() + i + 1, { k, que[i].e + 1, que[i].e + c });
50                         flag = true;
51                         break;
52                     }
53                 }
54             }
55
56             if (!flag) {
57                 que.erase(que.begin() + idx);
58                 k--;
59                 f = false;
60             }
61             else {
62                 f = true;
63             }
64         }
65     }
66
67     sort(que.begin(), que.end(), [](const chunk &ch1, const chunk &ch2) {
68         return ch1.v < ch2.v;
69     });
70     for (int k = 0; k < que.size(); ++k) {
71         printf("%d %d\n", que[k].v, que[k].s);
72     }
73 }
时间: 2024-10-15 06:28:05

hihoCoder #1198 Memory Allocating Algorithm的相关文章

hihocoder1198 Memory Allocating Algorithm(链表~)

题意: 小Hi和小Ho最近在研究内存分配的机制,他们写了一个比较简单的内存.内存可以表示成M个连续的存储空间,下标为0..M-1: 每当有数据写入时,内存分配程序会从下标0开始向右找一块足够存放下该数据的区域,将该数据写入.比如写入一个长度为2的数据,因为是第一个数据,我们用1来表示: 之后继续依次写入长度为3的数据和长度为2的数据,则有: 当数据足够多后,我们可能会遇到剩下的空间不足以写下新的数据.这时内存程序会从最早的数据开始进行删除.假设我们现在写到第8个数据把内存写满了: 这时我们需要写

PatentTips - Method to manage memory in a platform with virtual machines

BACKGROUND INFORMATION Various mechanisms exist for managing memory in a virtual machine environment. A virtual machine platform typically executes an underlying layer of software called a virtual machine monitor (VMM) which hosts one to many operati

Buddy system伙伴分配器实现

wikipedia:http://en.wikipedia.org/wiki/Buddy_memory_allocation The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible. This system makes us

linux下编译安装php各种报错大集合

PHP开源脚本语言 PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域.PHP的文件后缀名为php. 本文为大家整理汇总了一些linux下编译安装php各种报错大集合 ,感兴趣的同学参考下. 报错1:make 后报错如下: Generating phar.php /home/oldboy/tools/php-5.3.27

关于并行计算的Scan操作

simple and common parallel algorithm building block is the all-prefix-sums operation. In this chapter, we define and illustrate the operation, and we discuss in detail its efficient implementation using NVIDIA CUDA. Blelloch (1990) describes all-pref

ubuntu安装php常见错误集锦

以下错误摘录自 http://www.68idc.cn/help/jiabenmake/qita/20141114128775.html  .http://zgadzaj.com/how-to-install-php-53-and-52-together-on-ubuntu-1204 一.configure 报错 1.错误类型: Configure: error: Please reinstall the libcurl distribution-easy.h should be in <cur

GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败

使用GDALWarp写了一个裁切图像的算法.在小内存的电脑没事,大内存的电脑就处理失败(32位也没问题),查看GDAL的日志发现以下的错误信息: Fri Apr 08 17:39:02 2016: GDAL: GDALOpen(E:/Out/TRIPLESAT_1_PAN_L1_20160330024710_000315VI_005.tif, this=000000000508EB40) succeeds as GTiff. Fri Apr 08 17:39:02 2016: GDAL: GDA

防御导弹算法

算法效率绝对是最烂的(大鸟无喷),时间空间效率完全没有考虑,但是可能是最直观的,最白痴的思路.没有运用XXX算法思想,就是保证没读过算法相关书籍的任何人都能读懂. 题目出处:http://www.programfan.com/acm/show.asp?qid=5 题目如下: 防御导弹 Problem 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统. 但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够达到任意的高度,但是以后每一发炮弹都不能高于前一发的高度. 某天,雷达捕捉到敌国的导弹来袭

boost.lambda

// boost.lambda表达式用法 // made by davidsu33 // 2014-9-22 #include "stdafx.h" #include <boost/typeof/typeof.hpp> #include <boost/lambda/lambda.hpp> #include <boost/bind/bind.hpp> #include <boost/assign.hpp> #include <boos