【NOI1999】【BZOJ3117】内存分配

3117: [Noi1999]内存分配

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 19 Solved: 12
[Submit][Status][Discuss]
Description

内存是计算机重要的资源之一,程序运行的过程中必须对内存进行分配。
经典的内存分配过程是这样进行的:
1. 内存以内存单元为基本单位,每个内存单元用一个固定的整数作为标识,称为地址。地址从0开始连续排列,地址相邻的内存单元被认为是逻辑上连续的。我们把从地址i开始的s个连续的内存单元称为首地址为i长度为s的地址片。
2. 运行过程中有若干进程需要占用内存,对于每个进程有一个申请时刻T,需要内存单元数M及运行时间P。在运行时间P内(即T时刻开始,T+P时刻结束),这M个被占用的内存单元不能再被其他进程使用。
3、假设在T时刻有一个进程申请M个单元,且运行时间为P,则:
1. 若T时刻内存中存在长度为M的空闲地址片,则系统将这M个空闲单元分配给该进程。若存在多个长度为M个空闲地址片,则系统将首地址最小的那个空闲地址片分配给该进程。
2. 如果T时刻不存在长度为M的空闲地址片,则该进程被放入一个等待队列。对于处于等待队列队头的进程,只要在任一时刻,存在长度为M的空闲地址片,系统马上将该进程取出队列,并为它分配内存单元。注意,在进行内存分配处理过程中,处于等待队列队头的进程的处理优先级最高,队列中的其它进程不能先于队头进程被处理。
现在给出一系列描述进程的数据,请编写一程序模拟系统分配内存的过程。
Input

第一行是一个数N,表示总内存单元数(即地址范围从0到N-1)。从第二行开始每行描述一个进程的三个整数T、M、P(M <= N)。最后一行用三个0表示结束。
数据已按T从小到大排序。
输入文件最多10000行,且所有数据都小于109。
输入文件中同一行相邻两项之间用一个或多个空格隔开。
Output

包括2行。
第一行是全部进程都运行完毕的时刻。
第二行是被放入过等待队列的进程总数。
Sample Input

10

1 3 10

2 4 3

3 4 4

4 1 4

5 3 4

0 0 0

Sample Output

12

2

链表模拟一下。
用链表搞出现在正在处理的进程,每个进程占用的内存地址l,r
建堆存储一下正在运行的进程,关键字是结束时间.
然后暴力…
跑的飞起= =虽然算起来复杂度不太对
但是感觉卡不住啊…
英文注释显得我不是那么傻逼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define MAXINT 0x12ffffffffffffLL//max long long
#define Check   (ch>='0'&&ch<='9')
#define MAXN 10010
#define LL long long
using namespace std;
int n,top,maxnum;
LL T,M,P;
LL Tim = -1,last = 0;//last is the end time
int Head,num,HEAD;//num is the maxnum of the waiting queue
struct unit//running
{
    unit *next;
    LL l,r;
};
struct Pro
{
    LL t,m,p,l,r;
    bool operator <(const Pro& a)const
    {
        return t > a.t;
    }
}proce[MAXN],wait[MAXN];//proce have not appear   wait have not solve
void read(LL &x)
{
    char ch = getchar();x = 0;
    while (!Check)  ch = getchar();
    while (Check)   x = x * 10 + ch - '0',ch = getchar();
}
bool in()
{
    read(T);read(M);read(P);
    if  (T == 0 && M == 0 && P == 0)    return 0;
    proce[top].t = T;proce[top].m = M;proce[top++].p = P;
    return 1;
}
priority_queue<Pro> que;//when will these running task end
int main()
{
    freopen("memory.in","r",stdin);
    freopen("memory.out","w",stdout);
    scanf("%d",&n);
    while (in());
    proce[top].t = MAXINT;
    unit *head,*now,*pre,*t;
    head=new unit;head->l = -1;head->r = -2;head->next = NULL;//init list's head&tail
    head->next = new unit;head->next->l = 0;head->next->r = n - 1;head->next->next = NULL;
    head->next->next = new unit;head->next->next->l = n+1;head->next->next->r = n;head->next->next->next = NULL;
    Pro temp;temp.t = MAXINT;temp.l = temp.r = 0;
    que.push(temp);
    while   (Tim != MAXINT)
    {
        while   (Tim == que.top().t)
        {
            now = head;
            while   (now->next->r < que.top().l)
            {
                pre = now;
                now = now->next;
            }
            now->next = new unit((unit){now->next,que.top().l,que.top().r});//insert
            if  (now->r == que.top().l - 1) pre->next = now->next,now->next->l = now->l;//union
            if  (now->next->next->l == que.top().r + 1) now->next->r = now->next->next->r,now->next->next = now->next->next->next;
            que.pop();
        }

        while (Head < num)//solve watings
        {
            now = head;
            while   (now->next != NULL && now->r - now->l + 1 < wait[Head].m)   
            {
                pre = now;
                now = now->next;    
            }
            if  (now->next != NULL)
            {
                temp.t=Tim + wait[Head].p;temp.l = now->l;temp.r = now->l + wait[Head].m - 1;
                que.push(temp);
                if  (now->r - now->l + 1 == wait[Head].m)   pre->next = now->next;
                else    now->l += wait[Head].m;
            }
            else    break;//can't solve the first one so have to break
            Head++;
        }

        while   (proce[HEAD].t == Tim)//solve others
        {
            now = head;
            while   (now->next != NULL && now->r - now->l + 1 < proce[HEAD].m)  
            {
                pre = now;
                now = now->next;
            }
            if  (now->next != NULL)
            {
                temp.t=Tim + proce[HEAD].p;temp.l = now->l;temp.r = now->l + proce[HEAD].m - 1;
                que.push(temp);
                if  (now->r - now->l + 1 == proce[HEAD].m)  pre->next = now->next;
                else    now->l += proce[HEAD].m;
            }
            else//can't solve
            {
                temp.m = proce[HEAD].m;temp.p = proce[HEAD].p;
                wait[num++] = temp;
            }   
            HEAD++;
        }
        last = Tim;
        Tim = min(que.top().t,proce[HEAD].t);
    }
    cout<<last<<endl<<num;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值