hdu 2871 Memory Control (线段树&vector)

Memory Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3900    Accepted Submission(s): 936


Problem Description
Memory units are numbered from 1 up to N.
A sequence of memory units is called a memory block.
The memory control system we consider now has four kinds of operations:
1.  Reset Reset all memory units free.
2.  New x Allocate a memory block consisted of x continuous free memory units with the least start number
3.  Free x Release the memory block which includes unit x
4.  Get x Return the start number of the xth memory block(Note that we count the memory blocks allocated from left to right)
Where 1<=x<=N.You are request to find out the output for M operations.
 

Input
Input contains multiple cases.
Each test case starts with two integer N,M(1<=N,M<=50000) ,indicating that there are N units of memory and M operations.
Follow by M lines,each line contains one operation as describe above.
 

Output
For each “Reset” operation, output “Reset Now”.
For each “New” operation, if it’s possible to allocate a memory block,
output “New at A”,where Ais the least start number,otherwise output “Reject New”.
For each “Free” operation, if it’s possible to find a memory block occupy unit x,
output “Free from A to B”,where A and B refer to the start and end number of the memory block,otherwise output “Reject Free”.
For each “Get” operation, if it’s possible to find the xth memory blocks,
output “Get at A”,where A is its start number,otherwise output “Reject Get”.
Output one blank line after each test case.
 

Sample Input
  
  
6 10 New 2 New 5 New 2 New 2 Free 3 Get 1 Get 2 Get 3 Free 3 Reset
 

Sample Output
  
  
New at 1 Reject New New at 3 New at 5 Free from 3 to 4 Get at 1 Get at 5 Reject Get Reject Free Reset Now
 

Source
 

Recommend
gaojie
 

题意:

这题和poj3667类似。

点击打开链接

就是多了一些操作。

对于题目的Reset操作。直接区间更新加个lazy标记就行了。

对于New x直接和hotel的分配房间一样的处理就不说了见我博客。

但是要用一个vector容器来保存分配区间的左右端点。

所以先二分位置按左端点的顺序排。对于二分就用算法模板

upper_bound()函数来实现。

对于Free x操作。先找到x对应的区间。还是二分。如果找到了就区间更新。然后把端点从vector中移除。

对于Get x就简单了。如果x合理就直接输出向量第x个元素的左端点就行了。

iterator lower_bound( const key_type &key ): 返回一个 迭代器,指向 键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向 键值> key的第一个元素。
详细见代码:

#include <iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=50100;

struct node//区间结点
{
    int l,r;
};
vector<node>  used;
vector<node>::iterator p;
int ml[maxn<<2],mr[maxn<<2],ma[maxn<<2];
int st[maxn<<2];
int n,m;

bool cmp(node a,node b)//比较函数用于二分查找
{
    return a.l<b.l;
}
void btree(int L,int R,int k)//建树
{
    int ls,rs,mid;
    ma[k]=ml[k]=mr[k]=R-L+1;//初始化。ma为区间内最大连续房间数。ml为区间左端最大连续房间数。mr为右端。。
    st[k]=0;//0表示无标记。1表示区间内房间全被占用。-1表示区间房间全空闲
    if(L==R)
        return;
    ls=k<<1;
    rs=k<<1|1;
    mid=(L+R)>>1;
    btree(L,mid,ls);
    btree(mid+1,R,rs);
}
void change(int L,int R,int k)//根据状态修改结点信息
{
    if(st[k]==1)//占用
        ml[k]=mr[k]=ma[k]=0;
    else
        ml[k]=mr[k]=ma[k]=R-L+1;
}
void pushdown(int L,int R,int k)//下传标记
{
    int ls,rs,mid;
    ls=k<<1;
    rs=k<<1|1;
    mid=(L+R)>>1;
    if(st[k]==1)//占用
    {
        st[ls]=st[rs]=1;
        ml[ls]=mr[ls]=ma[ls]=0;
        ml[rs]=mr[rs]=ma[rs]=0;
    }
    else
    {
        st[ls]=st[rs]=-1;
        ml[ls]=mr[ls]=ma[ls]=mid-L+1;
        ml[rs]=mr[rs]=ma[rs]=R-mid;
    }
    st[k]=0;
}
void check(int L,int R,int l,int r,int k,int s)
{
    int ls,rs,mid;

    if(l==L&&r==R)
    {
        st[k]=s;
        change(L,R,k);
        return;
    }
    ls=k<<1;
    rs=k<<1|1;
    mid=(L+R)>>1;
    if(st[k])
        pushdown(L,R,k);
    if(l>mid)
        check(mid+1,R,l,r,rs,s);
    else if(r<=mid)
        check(L,mid,l,r,ls,s);
    else
    {
        check(L,mid,l,mid,ls,s);
        check(mid+1,R,mid+1,r,rs,s);
    }
    ma[k]=max(ma[ls],ma[rs]);
    ma[k]=max(ma[k],mr[ls]+ml[rs]);//找到最大区间
    ml[k]=ml[ls];//最基本的值
    mr[k]=mr[rs];
    if(ma[ls]==mid-L+1)//若右区间全满
        ml[k]+=ml[rs];//可能变成的值
    if(ma[rs]==R-mid)
        mr[k]+=mr[ls];
}
int qu(int L,int R,int k,int cost)//传空间大小返回连续区间起始位置
{
    int ls,rs,mid;
    if(cost>ma[k])
        return 0;
    if(L==R)
        return L;
    ls=k<<1;
    rs=k<<1|1;
    mid=(L+R)>>1;
    if(st[k])
        pushdown(L,R,k);
    if(cost<=ma[ls])//优先取左边编号
        return qu(L,mid,ls,cost);
    else if(cost<=mr[ls]+ml[rs])
        return mid-mr[ls]+1;
    else
        return qu(mid+1,R,rs,cost);
}
int main()
{
    int i,st,x;
    char com[20];
    node tt;
    while(~scanf("%d%d",&n,&m))
    {
        btree(1,n,1);
        used.clear();
        for(i=1; i<=m; i++)
        {
            scanf("%s",com);
            if(com[0]=='R')
            {
                check(1,n,1,n,1,-1);
                used.clear();
                printf("Reset Now\n");
                continue;
            }
            scanf("%d",&x);
            if(com[0]=='N')
            {
                st=qu(1,n,1,x);
                if(st)
                {
                    printf("New at %d\n",st);
                    check(1,n,st,st+x-1,1,1);
                    tt.l=st;
                    tt.r=st+x-1;
                    p=upper_bound(used.begin(),used.end(),tt,cmp);//找到第一个大于st的元素位置
                    used.insert(p,tt);
                }
                else
                    printf("Reject New\n");
            }
            else if(com[0]=='F')
            {
                tt.l=tt.r=x;
                p=upper_bound(used.begin(),used.end(),tt,cmp);//找到第一个大于x的位置
                st=p-used.begin()-1;
                if(st==-1||used[st].r<x)
                    printf("Reject Free\n");
                else
                {
                    printf("Free from %d to %d\n",used[st].l,used[st].r);
                    check(1,n,used[st].l,used[st].r,1,-1);
                    used.erase(used.begin()+st);
                }
            }
            else
            {
                if(x>used.size())
                    printf("Reject Get\n");
                else
                    printf("Get at %d\n",used[x-1].l);
            }
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值