P2894 [USACO08FEB]酒店Hotel

https://www.luogu.org/problem/show?pid=2894

题目描述

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 (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。

输入输出格式

输入格式:

  • 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.

参考样例,第一行输入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 退房,即让房间为空。

输入输出样例

输入样例#1:

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:

1
4
7
0
5
区间最长连续空位,类似最大子段和 最大子段和:http://www.cnblogs.com/TheRoadToTheGold/p/6360224.html
但这里区间合并更新max_l时,要判断左区间的sum是否等于max,跟最大子段和不一样
max_r同理
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,p,x,y,opl,opr;
struct node
{
int l,r,max,max_l,max_r,sum,flag;
}e[*];
void build(int k,int l,int r)
{
e[k].l=l;e[k].r=r;
e[k].sum=e[k].max=e[k].max_l=e[k].max_r=r-l+;
if(l==r) return;
int mid=l+r>>;
build(k<<,l,mid);build((k<<)+,mid+,r);
}
void down(int k)
{
e[k<<].flag=e[(k<<)+].flag=e[k].flag;
if(e[k].flag==)
e[k<<].max=e[k<<].max_l=e[k<<].max_r=e[(k<<)+].max=e[(k<<)+].max_l=e[(k<<)+].max_r=;
else
{
e[k<<].max=e[k<<].max_l=e[k<<].max_r=e[k<<].sum;
e[(k<<)+].max=e[(k<<)+].max_l=e[(k<<)+].max_r=e[(k<<)+].sum;
}
e[k].flag=;
}
int ask(int k)
{
if(e[k].l==e[k].r) return e[k].l;
if(e[k].flag) down(k);
if(e[k<<].max>=x) return ask(k<<);
int mid=e[k].l+e[k].r>>;
if(e[(k<<)+].max_l+e[k<<].max_r>=x) return mid-e[k<<].max_r+;
return ask((k<<)+);
}
void up(int k)
{
e[k].max=max(max(e[k<<].max,e[(k<<)+].max),e[k<<].max_r+e[(k<<)+].max_l);
if(e[k<<].sum==e[k<<].max) e[k].max_l=e[k<<].sum+e[(k<<)+].max_l;
else e[k].max_l=e[k<<].max_l;
if(e[(k<<)+].sum==e[(k<<)+].max) e[k].max_r=e[(k<<)+].sum+e[k<<].max_r;
else e[k].max_r=e[(k<<)+].max_r;
}
void change(int k,int f)
{
if(e[k].l>=opl&&e[k].r<=opr)
{
if(f==)
{
e[k].max=e[k].max_l=e[k].max_r=;
e[k].flag=;
return;
}
else
{
e[k].max=e[k].max_l=e[k].max_r=e[k].sum;
e[k].flag=;
return;
}
}
if(e[k].flag) down(k);
int mid=e[k].l+e[k].r>>;
if(opr<=mid) change(k<<,f);
else if(opl>mid) change((k<<)+,f);
else
{
change(k<<,f);
change((k<<)+,f);
}
up(k);
}
int main()
{
scanf("%d%d",&n,&m);
build(,,n);
for(int i=;i<=m;i++)
{
scanf("%d",&p);
if(p==)
{
scanf("%d",&x);
if(e[].max<x) printf("0\n");
else
{
opl=ask();
printf("%d\n",opl);
opr=opl+x-;
change(,);
}
}
else
{
scanf("%d%d",&opl,&y);
opr=opl+y-;
change(,);
}
}
}

自己做区间查询写不出来

#include<cstring>
#include<algorithm>
using namespace std;
int n,m,p,x,y,ans,ok;
struct node
{
int l,r,start,max,max_l,max_r;
}e[*];
void build(int k,int l,int r)
{
e[k].l=l,e[k].r=r;
e[k].max=r-l+;
e[k].max_l=r-l+;
e[k].max_r=r-l+;
if(l==r) return;
int mid=l+r>>;
build(k<<,l,mid);
build((k<<)+,mid+,r);
}
void ask(int k,int &longg,int & long_l,int & long_r)
{
if(!e[k].max) return;
if(e[k<<].max>=x)
{
if(ans>e[k<<].start)
{
anse[k<<].start;
ask(k<<);
}
}
int mid=e[k].l+e[k].r>>;
else if(e[(k<<)+].max>=x)
{
if(ans>e[(k<<)+].start)
{
ans=ans,e[(k<<)+].start;
ask((k<<)+);
}
} }
int main()
{
scanf("%d%d",&n,&m);
build(,,n);
for(int i=;i<=m;i++)
{
scanf("%d",&p);
if(p==)
{
scanf("%d",&x);
ans=0x7fffffff,ok=true;
int longg=,long_l=,long_r=;
ask(,longg,long_l,long_r);
}
}
}

思维定势的错误的区间查询代码

洛谷P2894 [USACO08FEB]酒店Hotel的更多相关文章

  1. 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  2. 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]

    题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...

  3. 洛谷 P2894 [USACO08FEB]酒店Hotel

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  4. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

  5. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  6. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  7. 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...

  8. 洛谷 P2894 [USACO08FEB]酒店

    题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...

  9. 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel

    题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...

随机推荐

  1. Bzoj2134:单选错位

    题面 Bzoj Sol 第\(i\)道题选对的概率就是\(\frac{min(a[i-1], a[i])}{a[i]*a[i-1]}\) # include <bits/stdc++.h> ...

  2. 来谈谈JAVA面向对象 - 鲁班即将五杀,大乔送他回家??

    开发IDE为Eclipse或者MyEclipse. 首先,如果我们使用面向过程的思维来解决这个问题,就是第一步做什么,第二步做什么? 鲁班即将五杀,大乔送他回家 这个现象可以简单地拆分为两步,代码大概 ...

  3. Centos samba 服务配置

    1背景 转到Linux有段时间了,vim操作还不能应对工程代码,之前一直都是Gnome桌面 + Clion 作开发环境,无奈在服务器上没有这样的环境, 看同事是(Windows)Source Insi ...

  4. 利用Cglib实现AOP

    前文讲了, 可以利用Spring, Guice等框架提供的容器实现AOP, 如果想绕过容器, 直接注入Class, 可以利用Cglib为对象加上动态代理,实现代码切入, 但是每次调用比较繁琐, 因此我 ...

  5. 洛谷 P1017 进制转换

    推荐洛谷 题目描述 我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1)为指数,以10为底数的幂之和的形式.例如:123可表示为 1*10^2+2*10^1+ ...

  6. 关于一道面试题【字符串 '1 + (5 - 2) * 3',怎么算出结果为10,'eval'除外】

    最近徘徊在找工作和继续留任的纠结之中,在朋友的怂恿下去参加了一次面试,最后一道题目是: 写一个函数,输入一个字符串的运算式,返回计算之后的结果.例如这样的: '1 + (5 - 2) * 3',计算出 ...

  7. java序列化浅谈

    首先大家进来第一个疑问肯定是"什么是序列化?为什么要使用序列化?怎么实现一个简单的序列化案例?" 1.序列化就是把对象以一种规范的二进制形式存在内存中,另一边以反序列化方式获取: ...

  8. if语句2017-03-17

    1.If语句: if(判断条件){ 判断为true的时候执行的语句 } else{ 判断为false的时候执行的语句 } 2.If else语句:        if(判断条件){ 判断为true的时 ...

  9. 深度学习基础网络 ResNet

    Highway Networks 论文地址:arXiv:1505.00387 [cs.LG] (ICML 2015),全文:Training Very Deep Networks( arXiv:150 ...

  10. PHP MVC框架核心类

    PHP MVC框架核心类 现在我们举几个核心框架的例子演示:在framework/core下建立一个Framework.class.php的文件.写入以下代码: // framework/core/F ...