洛谷 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& ...
随机推荐
- TP5部署服务器问题总结
及最近部署TP5遇到了很多坑,各种环境下都会出现一些问题,下面是我记录的排坑之路 先说最简单的lnmp一键安装包,我用的是1.5稳定版 安装命令:wget http://soft.vpser.net/ ...
- YII2 多MongoDB配置和使用
1:在config/web.php 文件下配置多个连接即可: 注意在componets 下 'mongodb' => [ 'class' => '\yii\mongodb\Connecti ...
- Hadoop(23)-Yarn资源调度器
Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于运行于操作系统之上的应用程序 1. Yarn工作机制 机制详解 第1 ...
- 密码发生器 南阳acm519
密码发生器 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如 ...
- 014---Django的中间件
前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装 ...
- Go生成UUID
Go生成UUID 在实际项目中,是经常会使用到一个唯一标识的,比如唯一标识一条记录等,使用C#得到唯一标识是很容易的.例 string guid = Guid.NewGuid().ToString() ...
- R语言学习笔记(十四):零碎知识点(41-45)
41--ls( ) ls()可以用来列出现存的所有对象. pattern是一个具名参数,可以列出所有名称中含有字符串"s"的对象. > ls() [1] "s&qu ...
- python2.7入门---SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib提 ...
- 第5模块闯关Bootstrap
“行(row)”必须包含在 .container (固定宽度)或 .container-fluid (100% 宽度)中,以便为其赋予合适的排列(aligment)和内补(padding). 通过“行 ...
- 【C#】 RBAC 权限框架
[C#] RBAC 权限框架 一. 名词解释 1. 用户 : 登录的账号, 和角色挂钩,可拥有多个角色 2. 角色 : 账号所属的角色, 和权限挂钩,可拥有多个权限 3. 权限 : 角色拥有的操作权限 ...