洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
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.
线段树维护 区间最长0,离左端点的0个数,离右端点0的个数,就可以快速合并啦
Code:
#include <cstdio>
#include <cstring>
#define ls id<<1
#define rs id<<1|1
int max(int x,int y){return x>y?x:y;}
const int N=1e5+10;
int lcnt[N<<2],rcnt[N<<2],len[N<<2],lazy[N<<2],n,m;
void updata(int id,int l,int r)
{
int mid=l+r>>1,lenl=mid+1-l,lenr=r-mid;
lcnt[id]=lcnt[ls];
if(lcnt[ls]==lenl) lcnt[id]=max(lcnt[ls],lenl+lcnt[rs]);
rcnt[id]=rcnt[rs];
if(rcnt[rs]==lenr) rcnt[id]=max(rcnt[rs],lenr+rcnt[ls]);
len[id]=max(rcnt[ls]+lcnt[rs],max(len[ls],len[rs]));
}
void push_down(int id,int l,int r)
{
if(~lazy[id])
{
int mid=l+r>>1,lenl=mid+1-l,lenr=r-mid;
lcnt[ls]=rcnt[ls]=len[ls]=(!lazy[id])*lenl;
lcnt[rs]=rcnt[rs]=len[rs]=(!lazy[id])*lenr;
lazy[ls]=lazy[rs]=lazy[id];
lazy[id]=-1;
}
}
void change(int id,int l,int r,int L,int R,int c)
{
if(L!=R) push_down(id,L,R);
if(l==L&&r==R)
{
lcnt[id]=rcnt[id]=len[id]=(!c)*(r+1-l);
lazy[id]=c;
return;
}
int Mid=L+R>>1;
if(r<=Mid) change(ls,l,r,L,Mid,c);
else if(l>Mid) change(rs,l,r,Mid+1,R,c);
else change(ls,l,Mid,L,Mid,c),change(rs,Mid+1,r,Mid+1,R,c);
updata(id,L,R);
}
int query(int id,int l,int r,int x)
{
if(l==r) return l;
push_down(id,l,r);
int mid=l+r>>1;
if(len[ls]>=x) return query(ls,l,mid,x);
else if(rcnt[ls]+lcnt[rs]>=x) return mid+1-rcnt[ls];
else return query(rs,mid+1,r,x);
}
int main()
{
scanf("%d%d",&n,&m);
memset(lazy,-1,sizeof(lazy));
lcnt[1]=rcnt[1]=len[1]=n,lazy[1]=0;
for(int op,x,y,i=1;i<=m;i++)
{
scanf("%d%d",&op,&x);
if(op==1)
{
if(len[1]<x) printf("0\n");
else
{
int pos;
printf("%d\n",pos=query(1,1,n,x));
change(1,pos,pos+x-1,1,n,1);
}
}
else
scanf("%d",&y),change(1,x,x+y-1,1,n,0);
}
return 0;
}
2018.8.30
洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告的更多相关文章
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...
- 洛谷P2894[USACO08FEB]酒店Hotel(线段树)
问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 「洛谷P1402」酒店之王 解题报告
P1402 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只 ...
- 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...
- 洛谷 P2894 [USACO08FEB]酒店
题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...
随机推荐
- 使用deque保留有限的记录
# 使用deque保留有限的记录 >>> from collections import deque >>> q = deque(maxlen=3) # 指定队列的 ...
- pycharm中每次创建py文件时就自动生成代码头,以及出现SyntaxError:Non-ASCII 。。。问题
我们在pycharm中执行py文件的时候,可能会出现以下错误 这是因为你没有制定编码格式,这时候你需要在文件最开始制定编码格式,代码如下 #!/user/bin/env python #-*- cod ...
- ruby Time类与Date类
Time类用于表示时间.时间除了表示年月日时分秒的信息外,还包含了表示地域时差的时区(time zone)信息.例如我们可以计算中国当前时间是国际协调时间的几点 Date类只用于表示年月日.因此,相对 ...
- YUM工具使用
一.yum命令概述: 1.简介: yum命令时在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务器自动下载R ...
- logger模块的使用
logging模块 下面是logger模块的配置文件,在写程序需要记录日志可以直接拿过来用,但是要经过相应配置的一些修改. 对于如何使用,在我上一篇随笔<ATM程序规范练习>中的记录日志的 ...
- 嵌入式框架Zorb Framework搭建七:任务的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- CentOs安装Mysql和配置初始密码
mysql官网yum安装教程,地址:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/#repo-qg-yum-fresh-install ...
- Spring事务:一种编程式事务,三种声明式事务
事务隔离级别 隔离级别是指若干个并发的事务之间的隔离程度.TransactionDefinition 接口中定义了五个表示隔离级别的常量: TransactionDefinition.ISOLATIO ...
- LeetCode:22. Generate Parentheses(Medium)
1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...
- 洛谷P1364 医院设置
LITTLESUN的第一道图论,撒花~~ 题目链接 这道题是Floyd的板子题 注意对于矩阵图的初始值赋值要全部赋值成最大值 十六进制的最大值表示方式是0x3f3f3f3f memset(G,0x3f ...