zoukankan      html  css  js  c++  java
  • 洛谷 P2894 [USACO08FEB]酒店Hotel

    题目描述

    The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

    The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

    Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

    Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

    参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

    若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

    若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

    输入输出格式

    输入格式:

    • Line 1: Two space-separated integers: N and M

    • Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

    输出格式:

    • Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

    输入输出样例

    输入样例#1:
    10 6
    1 3
    1 3
    1 3
    1 3
    2 5 5
    1 6
    
    输出样例#1:
    1
    4
    7
    0
    5
    

    线段树维护向左连续空的最大值,向右连续空的最大值,以前最长的空的

    屠龙宝刀点击就送

    #include <ctype.h>
    #include <cstdio>
    #define M 50005
    
    void read(int &x)
    {
        x=0;
        bool f=0;
        register char ch=getchar();
        for(; !isdigit(ch); ch=getchar()) if(ch=='-') f=1;
        for(; isdigit(ch); ch=getchar()) x=x*10+ch-'0';
        x=f?(~x)+1:x;
    }
    int n,m;
    struct treetype
    {
        int sum,l,r,Max,mid,flag;
        treetype *left,*right;
        treetype ()
        {
            left=right=NULL;
            l=r=sum=Max=flag=0;
        }
    }*root=new treetype;
    class typetree
    {
        private:
            int max(int a,int b) {return a<b?b:a;}
        public:
            void pushup(treetype *&k)
            {
                if(k->left->sum==k->left->Max) k->l=k->left->Max+k->right->l;
                else k->l=k->left->l;
                if(k->right->sum==k->right->Max) k->r=k->right->Max+k->left->r;
                else k->r=k->right->r;
                k->Max=max(max(k->left->Max,k->right->Max),k->left->r+k->right->l);
            }
            void build(treetype *&k,int l,int r)
            {
                k=new treetype;
                k->l=k->r=k->Max=k->sum=r-l+1;
                if(l==r) return;
                k->mid=(l+r)>>1;
                build(k->left,l,k->mid);
                build(k->right,k->mid+1,r);
            }
            void pushdown(treetype *&k)
            {
                k->left->flag=k->right->flag=k->flag;
                if(k->flag==1)
                {
                    k->left->r=k->left->l=k->left->Max=0;
                    k->right->r=k->right->l=k->right->Max=0;
                }
                else 
                {
                    k->left->Max=k->left->l=k->left->r=k->left->sum;
                    k->right->Max=k->right->l=k->right->r=k->right->sum;
                }
                k->flag=0;
            }
            void change(treetype *&k,int l,int r,int opt,int L,int R)
            {
                if(l>=L&&r<=R)
                {
                    k->flag=opt;
                    if(opt==1) k->l=k->r=k->Max=0;
                    else k->Max=k->l=k->r=k->sum;
                    return;
                }
                if(k->flag) pushdown(k);
                if(L<=k->mid) change(k->left,l,k->mid,opt,L,R);
                if(R>k->mid)  change(k->right,k->mid+1,r,opt,L,R); 
                pushup(k);
            }
            int Query(treetype *&k,int l,int r,int len)
            {
                if(l==r) return r;
                if(k->flag) pushdown(k);
                if(k->left->Max>=len) return Query(k->left,l,k->mid,len);
                if(k->left->r+k->right->l>=len) return k->mid-k->left->r+1;
                else return Query(k->right,k->mid+1,r,len);
            }
    };
    class typetree *abc;
    int main()
    {
        read(n);
        read(m);
        abc->build(root,1,n); 
        for(int opt,x,y;m--;)
        {
            read(opt);
            if(opt==1)
            {
                read(x);
                if(root->Max<x) {printf("0
    ");continue;} 
                int pos=abc->Query(root,1,n,x);
                printf("%d
    ",pos);
                abc->change(root,1,n,opt,pos,pos+x-1);
            }
            else 
            {
                read(x);
                read(y);
                abc->change(root,1,n,opt,x,x+y-1); 
            }
        }
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    sql字符串函数(转)
    sqlserver 导入/导出Excel
    SelectSingleNode和SelectNodes区别
    iOS sqlite 的各种操作
    iOS 自定义的对象类型的解档和归档
    开放-封闭原则(OCP)开-闭原则 和 依赖倒转原则,单一职责原则
    iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区
    iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
    iOS中的事件传递和响应者链条
    iOS开发 首次启动显示用户引导,第二次启动直接进入App,UIScrollView,UIPageControl,NSUserDefaults
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7299330.html
Copyright © 2011-2022 走看看