[USACO08FEB]酒店Hotel
2017-10-23 21:45
393 查看
题目描述
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
说的夸张一点,我两年才做会这一题(orz) 之前做的是小白逛公园
思路理解之后其实很清晰:线段树维护三个最大值
1.这个区间内最多的连续空房间的个数
2.这个区间从左端点开始向右的空房间的个数
3.这个区间从友端点开始向左的空房间的个数
而 1 又可以从子树的1,2 , 3过来
mmax 为1
lmax 为2
rmax 为3
因此有 f[x].max:=max{ max[ max(f[x*2].mmax,f[x*2+1].mmax),f[x*2].rmax+f[x*2+1].l) ] ,max(f[x].lmax,f[x].rmax) } (有点多自己理解下,不难)
如果左子树房间都是空的 则 f[x].lmax:=f[x*2].mmax+f[x*2+1].lmax
否则 f[x].lmax:=f[x*2].lmax
如果右子树房间都是空的 则 f[x].rmax:=f[x*2+1].mmax+f[x*2].rmax
否则 f[x].rmax:=f[x*2+1].rmax
其余的具体分析一下即可
代码就不解释了(话说学p的已经快绝迹了)
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
说的夸张一点,我两年才做会这一题(orz) 之前做的是小白逛公园
思路理解之后其实很清晰:线段树维护三个最大值
1.这个区间内最多的连续空房间的个数
2.这个区间从左端点开始向右的空房间的个数
3.这个区间从友端点开始向左的空房间的个数
而 1 又可以从子树的1,2 , 3过来
mmax 为1
lmax 为2
rmax 为3
因此有 f[x].max:=max{ max[ max(f[x*2].mmax,f[x*2+1].mmax),f[x*2].rmax+f[x*2+1].l) ] ,max(f[x].lmax,f[x].rmax) } (有点多自己理解下,不难)
如果左子树房间都是空的 则 f[x].lmax:=f[x*2].mmax+f[x*2+1].lmax
否则 f[x].lmax:=f[x*2].lmax
如果右子树房间都是空的 则 f[x].rmax:=f[x*2+1].mmax+f[x*2].rmax
否则 f[x].rmax:=f[x*2+1].rmax
其余的具体分析一下即可
代码就不解释了(话说学p的已经快绝迹了)
这里写代码片program df; type point=record a,b,c,d,lm,rm:longint; end; var i,j,n,m,x,y,z,k,t:longint; f:array[0..500000] of point; ff:boolean; function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end; procedure built(z,x,y:longint); var mid:longint; begin f[z].a:=x; f[z].b:=y; if x=y then begin f[z].c:=1; f[z].lm:=1; f[z].rm:=1; exit; end; mid:=(x+y) div 2; built(z*2,x,mid); built(z*2+1,mid+1,y); f[z].c:=max(max(f[z*2].c,f[z*2+1].c),f[z*2].rm+f[z*2+1].lm); f[z].c:=max(max(f[z].c,f[z].lm),f[z].rm); if f[z*2].c=f[z*2].b-f[z*2].a+1 then f[z].lm:=f[z*2].c+f[z*2+1].lm else f[z].lm:=f[z*2].lm; if f[z*2+1].c=f[z*2+1].b-f[z*2+1].a+1 then f[z].rm:=f[z*2+1].c+f[z*2].rm else f[z].rm:=f[z*2+1].rm; //writeln(x,' ',y,' ',f[z].c,' ',f[z].lm,' ',f[z].rm); end; procedure check(z:longint); var ans:longint; begin //if (f[x*2].c=(f[x*2].b-f[x*2].a)) then if f[z*2].c=f[z*2].b-f[z*2].a+1 then f[z].lm:=f[z*2].c+f[z*2+1].lm else f[z].lm:=f[z*2].lm; if f[z*2+1].c=f[z*2+1].b-f[z*2+1].a+1 then f[z].rm:=f[z*2+1].c+f[z*2].rm else f[z].rm:=f[z*2+1].rm; f[z].c:=max(max(f[z*2].c,f[z*2+1].c),f[z*2].rm+f[z*2+1].lm); f[z].c:=max(max(f[z].lm,f[z].rm),f[z].c); //writeln(f[z].a,' ',f[z].b,' ',f[z].c,' ',f[z].lm,' ',f[z].rm); end; procedure xiugai(x:longint); var l,r:longint; begin if f[x].d=0 then exit; if f[x].d=-1 then begin f[x*2].c:=0; f[x*2].lm:=0; f[x*2].rm:=0; f[x*2+1].c:=0; f[x*2+1].lm:=0; f[x*2+1].rm:=0; f[x*2].d:=-1; f[x*2+1].d:=-1; end else begin l:=f[x*2].a; r:=f[x*2].b; f[x*2].c:=r-l+1; f[x*2].lm:=r-l+1; f[x*2].rm:=r-l+1; l:=f[x*2+1].a; r:=f[x*2+1].b; f[x*2+1].c:=r-l+1; f[x*2+1].lm:=r-l+1; f[x*2+1].rm:=r-l+1; f[x*2].d:=1; f[x*2+1].d:=1; end; f[x].d:=0; end; procedure search(x,y:longint); var mid:longint; begin xiugai(x); if f[x].a=f[x].b then begin t:=f[x].a; ff:=true; exit; end; mid:=(f[x].a+f[x].b)>>1; if f[x*2].c>=y then search(x*2,y) else if ((f[x*2].rm+f[x*2+1].lm>=y) and (not ff)) then begin t:=mid-f[x*2].rm+1; ff:=true; exit; end else if ((f[x*2+1].c>=y) and (not ff)) then search(x*2+1,y); check(x); end; procedure deal(x,l,r:longint); var mid:longint; begin xiugai(x); if (f[x].a=l) and (f[x].b=r) then begin f[x].c:=r-l+1; f[x].lm:=r-l+1; f[x].rm:=r-l+1; f[x].d:=1; exit; end; mid:=(f[x].a+f[x].b) div 2; if l>mid then deal(x*2+1,l,r) else if r<=mid then deal(x*2,l,r) else begin deal(x*2,l,mid); deal(x*2+1,mid+1,r); end; check(x); //writeln(f[x].a,' ',f[x].b,' ',f[x].c,' ',f[x].lm,' ',f[x].rm); end; procedure check2(x,l,r:longint); var mid:longint; begin xiugai(x); if (f[x].a=l) and (f[x].b=r) then begin f[x].c:=0; f[x].lm:=0; f[x].rm:=0; f[x].d:=-1; //writeln(f[x].a,' ',f[x].b); exit; end; if f[x].a=f[x].b then exit; mid:=(f[x].a+f[x].b) div 2; if l>mid then check2(x*2+1,l,r) else if r<=mid then check2(x*2,l,r) else begin check2(x*2,l,mid); check2(x*2+1,mid+1,r); end; check(x); end; begin readln(n,m); built(1,1,n); for i:=1 to m do begin read(k); if k=1 then begin readln(x); ff:=false; if f[1].c<x then writeln(0) else begin t:=0; search(1,x); writeln(t); check2(1,t,t+x-1); end; end else begin readln(x,y); deal(1,x,x+y-1); end; end; end.
相关文章推荐
- 线段树 (区间合并)【p2894】[USACO08FEB]酒店Hotel
- P2894 [USACO08FEB]酒店Hotel
- 线段树 洛谷P2894 [USACO08FEB]酒店Hotel
- [USACO08FEB]酒店Hotel
- 洛谷 P2894 [USACO08FEB]酒店Hotel(线段树区间合并)
- luogu P2894 [USACO08FEB]酒店Hotel
- 洛谷P2894 [USACO08FEB]酒店Hotel
- [USACO08FEB]酒店Hotel
- P2894 [USACO08FEB]酒店Hotel
- 线段树【洛谷P2894】 [USACO08FEB]酒店Hotel
- 浅谈线段树 (例题:[USACO08FEB]酒店Hotel)By cellur925
- 上海求职驿站:http://www.shjobhotel.com/
- 过时的幽默 ---酒店小姐的幽默
- 酒店管理程序
- LA Hotel & Motel
- sydney airport hotel recommendations
- 酒店无线网络解决方案
- WinForm界面开发之酒店管理系统--开篇
- 酒店点餐系统开发详解(二)
- 酒店女继承人抨击酒店时弊