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 ...
随机推荐
- html5压缩图片并上传
手机端图片有很大的,上传的时候很慢,这时候就要压缩一下了,有一个开源的js可以压缩图片的大小,开源地址如下:https://github.com/think2011/localResizeIMG3 代 ...
- Spring2.5与JDK8的集成问题
Spring2.5不支持JDK8及其以上的版本,因为有一段校验JDK版本的代码,当JDK版本大于1.7之后,会识别成JDK1.4之前的.会报版本太低的错误. /* * Copyright 2002-2 ...
- 给 IIS Express 配置虚拟目录
使用 vs2015 打开旧项目,之前使用 iis 配置站点,然后在 vs 中附加 w3wp.exe 进行开发和调试的. 由于种种原因 iis 上配置站点各种失败. 之后发现,其实在 vs2015 中按 ...
- WCF 异步调用问题
添加引用时生成"勾选允许生成异步操作" Wcf异步调用三种方式: 第一种:直接调用异步方法 var serviceClient = new MyServiceClient(); s ...
- [转]Ubuntu 用vsftpd 配置FTP服务器
FROM : http://www.cnblogs.com/CSGrandeur/p/3754126.html 网上的文章好难懂啊..只想要简单粗暴,弄好能用就行啊,复杂的以后研究不行吗...折腾好久 ...
- linux输入输出重定向
http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 ...
- 矩形覆盖-我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
class Solution { public: int rectCover(int number) { ; ; ; ||number==) ; ) ; ;i<number+;i++){ res ...
- WebSocket使用教程 2
WebSocket使用教程 - 带完整实例 收藏 james_laughing 发表于 2年前 阅读 46438 收藏 23 点赞 5 评论 4 摘要: WebSocket使用教程 - 带完整实例 什 ...
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索
11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...
- [CareerCup] 14.3 Final Finally Finalize 关键字比较
14.3 What is the difference between final, finally, and finalize? 这道题考察我们Java中的三个看起来很相似的关键字final,fin ...