Codeforces 46D Parking Lot

时间:2022-12-15 21:56:53
D. Parking Lot
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Sample test(s)
Input
30 1 2
6
1 5
1 4
1 5
2 2
1 5
1 4
Output
0
6
11
17
23
Input
30 1 1
6
1 5
1 4
1 5
2 2
1 5
1 4
Output
0
6
11
17
6
Input
10 1 1
1
1 12
Output
-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的更多相关文章

  1. ●CodeForces 480E Parking Lot

    题链: http://codeforces.com/problemset/problem/480/E题解: 单调队列,逆向思维 (在线的话应该是分治做,但是好麻烦..) 离线操作,逆向考虑, 最后的状 ...

  2. Codeforces 219E Parking Lot 线段树

    Parking Lot 线段树区间合并一下, 求当前要占的位置, 不包括两端点的写起来方便一点. #include<bits/stdc++.h> #define LL long long ...

  3. Codeforces Round &num;135 &lpar;Div&period; 2&rpar; E&period; Parking Lot 线段数区间合并

    E. Parking Lot time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. Codeforces 480&period;E Parking Lot

    E. Parking Lot time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  5. 【26&period;8&percnt;】【CF 46D】Parking Lot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. Codeforces Parking Lot

    http://codeforces.com/problemset/problem/630/I 简单的排列组合,推式子技巧:举一个小样例,看着推,别抽象着推,容易错 #include <iostr ...

  7. Parking Lot CodeForces - 480E

    大意: 给定01矩阵, 单点赋值为1, 求最大全0正方形. 将询问倒序处理, 那么答案一定是递增的, 最多增长$O(n)$次, 对于每次操作暴力判断答案是否增长即可, 也就是说转化为判断是否存在一个边 ...

  8. Codeforces Round&num;415 Div&period;2

    A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...

  9. CF 480 E&period; Parking Lot

    CF 480 E. Parking Lot http://codeforces.com/contest/480/problem/E 题意: 给一个n*m的01矩阵,每次可以将一个0修改为1,求最大全0 ...

随机推荐

  1. Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)

    环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...

  2. net-snmp源码VS2013编译添加加密支持&lpar;OpenSSL&rpar;(在VS里配置编译OpenSSL)

    net-snmp源码VS2013编译添加加密支持(OpenSSL) snmp v3 协议使用了基于用户的安全模型,具有认证和加密两个模块. 认证使用的算法是一般的消息摘要算法,例如MD5/SHA等.这 ...

  3. 【转载】 ionic 的 下拉刷新 与 上拉加载

    这篇文章是讲解 Ioinc中怎么实现 下拉刷新和上拉加载的.也是我们日常做项目是必不可少的功能.有兴趣的小伙伴可以来学习一下. 更多关于 IONIC 的资源: http://www.aliyue.ne ...

  4. RequireJS使用及JS目录规划

    1.RequireJS学习文档: http://www.requirejs.cn/ 2.目录规划(参考:ColUdf.vm) 1)一个完整的html,对应一个js文件,模块js在主页面对应的js文件中 ...

  5. Sdut 2416 Fruit Ninja II&lpar;山东省第三届ACM省赛 J 题&rpar;&lpar;解析几何&rpar;

    Time Limit: 5000MS Memory limit: 65536K 题目描述 Haveyou ever played a popular game named "Fruit Ni ...

  6. Swift的基础之关于&OpenCurlyDoubleQuote;!”和&OpenCurlyDoubleQuote;?”的使用介绍

    swift编程,不外乎是定义属性或者函数(方法),访问属性或者调用函数,类型转换,?和!在这几个过程中,都有一展身手的时候,而且,每次要考虑使用的时候,它们俩都会一起出现在我们的大脑中,用还是不用,如 ...

  7. SoapUI测试WebService接口

      Getting Started Getting started with some ad-hoc testing of a SOAP service is straight forward; se ...

  8. 4&period;update更新和delete删除用法

    一.update更新 UserMapper.java package tk.mybatis.simple.mapper; import org.apache.ibatis.annotations.Pa ...

  9. 关于Java中子类调用父类方法

    当一个类继承于另一个类,子类中没有父类的方法时.用子类的对象调用方法时,会首先在子类中查找,如果子类中没有改方法,再到父类中查找. 当一个方法只在父类中定义时,调用该方法时会使用父类中的属性. 如果该 ...

  10. linux安装oracle的官方文档

    1:https://docs.oracle.com/cd/E11882_01/install.112/e47689/toc.htm 2:https://oracle-base.com/articles ...