Codeforces 46D Parking Lot
2 seconds
256 megabytes
standard input
standard output
Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as L meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of the tasks come from the driving is right side!). Every driver when parking wants to leave for themselves some extra space to move their car freely, that's why a driver is looking for a place where the distance between his car and the one behind his will be no less than b meters and the distance between his car and the one in front of his will be no less than f meters (if there's no car behind then the car can be parked at the parking lot segment edge; the same is true for the case when there're no cars parked in front of the car). Let's introduce an axis of coordinates along the pavement. Let the parking lot begin at point 0 and end at point L. The drivers drive in the direction of the coordinates' increasing and look for the earliest place (with the smallest possible coordinate) where they can park the car. In case there's no such place, the driver drives on searching for his perfect peaceful haven. Sometimes some cars leave the street and free some space for parking. Considering that there never are two moving cars on a street at a time write a program that can use the data on the drivers, entering the street hoping to park there and the drivers leaving it, to model the process and determine a parking lot space for each car.
The first line contains three integers L, b и f (10 ≤ L ≤ 100000, 1 ≤ b, f ≤ 100). The second line contains an integer n (1 ≤ n ≤ 100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the request type is equal to 1, then in that case the second number indicates the length of a car (in meters) that enters the street looking for a place to park. And if the request type is equal to 2, then the second number identifies the number of such a request (starting with 1) that the car whose arrival to the parking lot was described by a request with this number, leaves the parking lot. It is guaranteed that that car was parked at the moment the request of the 2 type was made. The lengths of cars are integers from 1 to 1000.
For every request of the 1 type print number -1 on the single line if the corresponding car couldn't find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone.
30 1 2
6
1 5
1 4
1 5
2 2
1 5
1 4
0
6
11
17
23
30 1 1
6
1 5
1 4
1 5
2 2
1 5
1 4
0
6
11
17
6
10 1 1
1
1 12
-1
Solution
模拟。
用pair<int,int>存空白区间,
用优先队列(priority queue)存(维护)所有空白区间。
这里有一个我遇到的问题:存(维护)何种空白区间。
显然有两种方案:
(1)存“实际”的空白区间,即(后车头/道路起点--前车尾/道路终点),停车时需考虑前后车距;
(2)存“可用”的空白区间,“可用”的含义是只要长度允许,车可在区间内任意停放,亦即不用考虑前后车距。
按方式(2),停车操作很方便实现,但离开操作就很麻烦(我在此处凌乱了,还没确认是否可做)。
按方式(1)则相反,但停车操作只是if-else,思路很清楚。
另外,还需要将当前活跃(active)区间用数组标记,将区间(a, b)记成 tail[a]=b, head[b]=a
#include<bits/stdc++.h>
#define X first
#define Y second
#define set1(a) memset(a, -1, sizeof(a))
#define remove(a) head[tail[a]]=-1, tail[a]=-1
#define renew(a, b) tail[a]=b, head[b]=a
using namespace std;
typedef pair<int,int> pii;
pii r[];
int L, b, f, n;
void input(){
scanf("%d%d%d%d", &L, &b, &f, &n);
for(int i=; i<=n; i++)
scanf("%d%d", &r[i].X, &r[i].Y);
} priority_queue<pii, vector<pii>, greater<pii> > q;
stack<pii> s;
const int MAX_L=1e5+;
int head[MAX_L], tail[MAX_L];
int ans[];
void park(int i){
int len=r[i].Y;
ans[i]=-;
while(!q.empty()){
pii top=q.top();
q.pop();
if(tail[top.X]!=top.Y) continue;
if(top.X==){
if(top.Y==L){
if(L>=len){
ans[i]=;
remove();
if(L>len){
q.push(pii(len, L));
//printf("%d %d\n", len, L);
renew(len, L);
}
}
}
else if(top.Y>=len+f){
ans[i]=;
remove();
q.push(pii(len, top.Y));
renew(len, top.Y);
}
}
else if(top.Y>=top.X+b+len){
if(top.Y==L){
remove(top.X);
renew(top.X, top.X+b);
ans[i]=top.X+b;
if(L>top.X+b+len){
q.push(pii(top.X+b+len, L));
renew(top.X+b+len, L);
}
}
else if(top.Y>=top.X+b+len+f){
remove(top.X);
renew(top.X, top.X+b);
ans[i]=top.X+b;
q.push(pii(top.X+b+len, top.Y));
renew(top.X+b+len, top.Y);
}
}
if(~ans[i]) break;
s.push(top);
}
while(!s.empty())
q.push(s.top()), s.pop();
} void leave(int i){
int lb=ans[i], rb=lb+r[i].Y;
int tmp;
if(~head[lb]) lb=head[lb], remove(lb);
if(~tail[rb]) tmp=tail[rb], remove(rb), rb=tmp; //error-prone
q.push(pii(lb, rb));
renew(lb, rb);
} void init(){
set1(head);
set1(tail);
q.push(pii(, L));
renew(, L);
} int main(){
//freopen("in", "r", stdin);
input();
init();
for(int i=; i<=n; i++)
if(r[i].X==) park(i), printf("%d\n", ans[i]);
else leave(r[i].Y);
return ;
}
P.S. 这道题的模拟也可以不用优先队列,用链表也行。
Codeforces 46D Parking Lot的更多相关文章
- ●CodeForces 480E Parking Lot
题链: http://codeforces.com/problemset/problem/480/E题解: 单调队列,逆向思维 (在线的话应该是分治做,但是好麻烦..) 离线操作,逆向考虑, 最后的状 ...
- Codeforces 219E Parking Lot 线段树
Parking Lot 线段树区间合并一下, 求当前要占的位置, 不包括两端点的写起来方便一点. #include<bits/stdc++.h> #define LL long long ...
- Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
E. Parking Lot time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 480.E Parking Lot
E. Parking Lot time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- 【26.8%】【CF 46D】Parking Lot
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Parking Lot
http://codeforces.com/problemset/problem/630/I 简单的排列组合,推式子技巧:举一个小样例,看着推,别抽象着推,容易错 #include <iostr ...
- Parking Lot CodeForces - 480E
大意: 给定01矩阵, 单点赋值为1, 求最大全0正方形. 将询问倒序处理, 那么答案一定是递增的, 最多增长$O(n)$次, 对于每次操作暴力判断答案是否增长即可, 也就是说转化为判断是否存在一个边 ...
- Codeforces Round#415 Div.2
A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...
- CF 480 E. Parking Lot
CF 480 E. Parking Lot http://codeforces.com/contest/480/problem/E 题意: 给一个n*m的01矩阵,每次可以将一个0修改为1,求最大全0 ...
随机推荐
- 单机最大tcp连接数
from:http://www.cnblogs.com/mydomain/archive/2013/05/27/3100835.html 单机最大tcp连接数 网络编程 在tcp应用中,server事 ...
- CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… 第1种效果: 代码如下: <div class="loading"> ...
- 【WPF】Winform调用WPF窗体注意事项
1.需要添加一些引用 2.调用处使用如下方法进行调用 Window win= new Window(); ElementHost.EnableModelessKeyboardInterop(win) ...
- C语言 数组类型与数组指针类型
//数组类型与数组指针类型 #include<stdio.h> #include<stdlib.h> #include<string.h> void main(){ ...
- 挖Linux中的古老缩略语
[2005-06-22 15:23][Nigel McFarlane][TechTarget] <<阅读原文>> Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世 ...
- wooyun本地数据抓取
---- #-*-coding:utf-8-*- import re import urllib import MySQLdb import time from urllib import unquo ...
- ASP.NET中进行消息处理(MSMQ) 二
在我上一篇文章<ASP.NET中进行消息处理(MSMQ)一>里对MSMQ做了个通俗的介绍,最后以发送普通文本消息和复杂的对象消息为例介绍了消息队列的使用. 本文在此基础上继续介绍MSMQ的 ...
- Uedit的快捷键
Key1 自动换行_CTRL + W 这个已经不是什么新奇的功能了,就连你们最不喜欢的notepad都有了这个功能.说来也奇怪,编辑器为什么都带有这个功能呢?谁愿意自己的编辑器带有水平滚动条啊 ...
- UltraEdit编辑器使用心得之正则表达式篇
ultraEdit 中通过Ctrl+R 可以快速进行文本替换等处理操作,如果在这中间用一些正则表达式那将帮助NI更高效的进行文字处理操作,相关正则表达式列述如下: % 匹配行首 - 表示搜索字符串必须 ...
- survival analysis 生存分析与R 语言示例 入门篇
原创博客,未经允许,不得转载. 生存分析,survival analysis,顾名思义是用来研究个体的存活概率与时间的关系.例如研究病人感染了病毒后,多长时间会死亡:工作的机器多长时间会发生崩溃等. ...