洛谷 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& ...
随机推荐
- Apache Maven(六):存储库
Maven 存储库主要是存放一些第三方依赖jar包等. 严格来说,只有两种存储库:本地和远程,本地存储库是指您远程下载到本地的一个缓存,还包含尚未发布的临时构建文件.远程存储库是指一些可以通过各种协议 ...
- 首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法
首层nginx(172.25.10.1):先获取真实IP($remote_addr),再将真实IP传递给X-Forwarded-For proxy_set_header X-Real-IP $r ...
- PHP 进阶篇:面向对象的设计原则,自动加载类,类型提示,traits,命名空间,spl的使用,反射的使用,php常用设计模式 (麦子学员 第三阶段)
以下是进阶篇的内容:面向对象的设计原则,自动加载类,类型提示,traits,命名空间,spl的使用,反射的使用,php常用设计模式 ================================== ...
- pygame小游戏之坦克大战
以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5 pycharm import pygame,sys,time from random ...
- ADB工具的安装
1.Windows ADB工具下载地址: https://developer.android.google.cn/studio/releases/platform-tools ADB工具官网教程: h ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- [BZOJ1040][ZJOI2008]骑士(树形DP)
对于一个联通块内,有且只有一个环,即n个点n条边 那么找到那个环,然后任意断一条边,这个联通块就变成一棵树了,然后做树形DP就行了 对于断的边要记录下来DP时特判 Code #include < ...
- P1855 榨取kkksc03
P1855 榨取kkksc03 题目描述 以下皆为真实的故事. 洛谷2的团队功能是其他任何oj和工具难以达到的.借助洛谷强大的服务器资源,任何学校都可以在洛谷上零成本的搭建oj并高效率的完成训练计划. ...
- java Vector向量
9.3.4 Vector向量 [专业IT培训机构,真正零首付入学www.bjsxt.com] Vector底层是用数组实现的List,相关的方法都加了同步检查,因此“线程安全,效率低”. 比如,ind ...
- C# String函数
public static bool IsNullOrEmpty(string value) 如果 true 参数为 value 或空字符串 (""),则为 null:否则为 fa ...