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 ...
随机推荐
- UITextView限制字数与行数
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSSt ...
- HTML5 web workes实现多线程
对多线程来说尽量使用HTML5的WEB WORKER特性 HTML5中的Web Worker是使用多个线程并发执行Javascript程序.另外,这种特别的多线程实现能减少困惑开发者多年的,在其他平台 ...
- 11Spring_AOP编程(AspectJ)_概述
AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut .对比前一种提到的传统的Aop编程,AspctJ更加的常用.Asp ...
- 给 IIS Express 配置虚拟目录
使用 vs2015 打开旧项目,之前使用 iis 配置站点,然后在 vs 中附加 w3wp.exe 进行开发和调试的. 由于种种原因 iis 上配置站点各种失败. 之后发现,其实在 vs2015 中按 ...
- python数字图像处理(13):基本形态学滤波
对图像进行形态学变换.变换对象一般为灰度图或二值图,功能函数放在morphology子模块内. 1.膨胀(dilation) 原理:一般对二值图像进行操作.找到像素值为1的点,将它的邻近像素点都设置成 ...
- Android 编程下 Touch 事件的分发和消费机制
Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- JNDI全面总结(zz)
原理: 在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...
- CodeForces 166E -Tetrahedron解题报告
这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...
- 『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)
原文转载自:http://hi.baidu.com/suming/item/81e45b1ab9b4585f2a3e2243 最近比较浅的研究了一下dataGridView的重绘,发现里面还是有很多东 ...
- ORCHARD中文文档(翻译)
众所周知,Orchard是.net领域最好的开源CMS之一,他使用了微软最先进的技术,有一群先进理念的支持者,但是,所有的事情在国内总得加个但是,Orchard也不例外,中文资料相对比较少,官网提供的 ...