【26.8%】【CF 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
【题解】
把车开走操作的x指的是n个操作里面的第x个操作对应的车。
这题好坑啊,也怪自己没看清题目。它说的是长度为L。。
0..L说的是点。然后相邻两个点之间的距离为1,这样就组成长度为L的一段路了-_-.
我们不要管他。就变成0..L-1
这样每个点就代表一个距离了。而不是真的是一个点。。
然后我们在这条路的前边扩充b,在后边扩充f
整个区间就变成[-b..L-1+f]了(线段树不管你区间是不是负都可以的^_^)
输入的车的长度是x
那么问题就转换成在这个区间内找一段x+b+f的连续空位置。
假设找到的最左端的位置是pos;
那么pos+b就是要输出的答案了。
然后占据的时候不是占据pos..pos+b+f+x
而应该占据pos+b..pos+x-1
但我觉得这个问题还是有点BUG的。
就是后面进来的车停在了前面进来的车的前面b单位长度处。那么如果f>b,前面那辆车不就不满足要求了吗。。。(路人:出题人就是爷,你管他呢)
找连续空位置的话。
记录llx[rt],rlx[rt],lx[rt]分别表示这个节点从最左开始连续的空位置数目,这个节点从最右开始连续的空位置数目,整个区间不管哪里,连续的空位置数目。
然后所求的连续空位置有3种可能。
1.全部在左区间
2.全部在右区间。
3.横跨两个区间。
所以得到lx[rt] = max(lx[rt<<1],lx[rt<<1|1],rlx[rt<<1]+llx[rt<<1|1]);
因为连续区间块的时候要尽量往左。
所以先递归左儿子。然后是横跨中间的情况(如果是这种情况就可以直接输出起始位置了);最后是右儿子;
【代码】
#include <cstdio>
#include <algorithm>
#define lson begin,m,rt<<1
#define rson m+1,end,rt<<1|1 using namespace std; const int MAXL = 101000; struct data2
{
int l,r;
}; data2 qujian[101];
int l, b, f, n;
int cover[MAXL * 4],llx[MAXL*4],rlx[MAXL*4],lx[MAXL*4]; void push_up(int rt,int len)
{
lx[rt] = max(lx[rt << 1], lx[rt << 1 | 1]);
lx[rt] = max(lx[rt], rlx[rt << 1] + llx[rt << 1 | 1]);
llx[rt] = llx[rt << 1];
if (llx[rt] == (len - (len >> 1))) //如果左区间全是连续的空位置。
llx[rt] += llx[rt << 1 | 1];//则加上右区间从最左开始的连续空位置数目。
rlx[rt] = rlx[rt << 1 | 1];
if (rlx[rt] == (len >> 1))
rlx[rt] += rlx[rt << 1];
} void build(int begin, int end, int rt)
{
cover[rt] = 0;
if (begin == end)
{
llx[rt] = rlx[rt] = lx[rt] = 1;
return;
}
int m = (begin + end) >> 1;
build(lson);
build(rson);
push_up(rt,end-begin+1);
} void input_data()
{
scanf("%d%d%d", &l, &b, &f);
build(-b, l + f -1, 1);
} void push_down(int rt,int len)
{
if (cover[rt] != -1)
{
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
if (cover[rt] == 1)
{
llx[rt << 1] = rlx[rt << 1] = lx[rt << 1] = 0;
llx[rt << 1| 1] = rlx[rt << 1 | 1] = lx[rt << 1 | 1] = 0;
}
else
{
llx[rt << 1] = rlx[rt << 1] = lx[rt << 1] = len - (len >> 1);
llx[rt << 1 | 1] = rlx[rt << 1 | 1] = lx[rt << 1 | 1] = len >> 1;
}
cover[rt] = -1;
}
} int query(int len, int begin, int end, int rt)
{
if (begin == end)
return begin;
push_down(rt,end - begin+1);
int m = (begin + end) >> 1;
if (lx[rt << 1] >= len)
return query(len, lson);
else
if (rlx[rt << 1] + llx[rt << 1 | 1] >= len)
return m - rlx[rt << 1] + 1; //返回的这个坐标可以手算模拟下。
else
return query(len, rson);
} void up_data(int l, int r, int num, int begin, int end, int rt) //用于更新节点。
{
if (l <= begin && end <= r)
{
cover[rt] = num;
if (num == 1)
{
llx[rt] = rlx[rt] = lx[rt] = 0;
return;
}
else
{
llx[rt] = rlx[rt] = lx[rt] = end - begin + 1;
}
return;
}
push_down(rt,end - begin+1);
int m = (begin + end) >> 1;
if (l <= m)
up_data(l, r, num, lson);
if (m < r)
up_data(l, r, num, rson);
push_up(rt, end - begin + 1);
} void output_ans()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int op, x;
scanf("%d%d", &op, &x);
if (op == 1)
{
int len = x + b + f;
if (len > lx[1])
printf("-1\n");
else
{
int qidian = query(len, -b, l + f - 1, 1);
int cl, cr;
cl = qidian + b; //[cl,cr]是要修改的区间。
cr = cl + x - 1;
qujian[i].l = cl;
qujian[i].r = cr;
up_data(cl, cr, 1, -b, l + f - 1, 1);
printf("%d\n", qidian+b);
}
}
else
{
int zuo, you;
zuo = qujian[x].l;
you = qujian[x].r;
up_data(zuo, you, 0, -b, l + f - 1, 1);
}
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
//freopen("F:\\rush_out.txt", "w", stdout);
input_data();
output_ans();
return 0;
}
【26.8%】【CF 46D】Parking Lot的更多相关文章
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)
cf真的难…… 点我浏览丧题. [A]Arpa和她对墨西哥人浪的研究 Arpa正在对墨西哥人浪进行研究. 有n个人站成一排,从1到n编号,他们从时刻0开始墨西哥人浪. 在时刻1,第一个人站起来.在时刻 ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- 【实战Java高并发程序设计6】挑战无锁算法:无锁的Vector实现
[实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference [实战Java高并发程序设计 3]带有时间戳的对象 ...
- 【实战Java高并发程序设计 3】带有时间戳的对象引用:AtomicStampedReference
[实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference AtomicReference无法解决上述问题的根 ...
- 【模拟题(电子科大MaxKU)】解题报告【树形问题】【矩阵乘法】【快速幂】【数论】
目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s ...
- 【Android】【录音】Android录音--AudioRecord、MediaRecorder
[Android][录音]Android录音--AudioRecord.MediaRecorder Android提供了两个API用于实现录音功能:android.media.AudioRecord. ...
随机推荐
- 2.技巧: 用 JAXM 发送和接收 SOAP 消息—Java API 使许多手工生成和发送消息方面必需的步骤自动化
转自:https://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html 技巧: 用 JAXM 发送和接收 SOAP 消息—Java ...
- 洛谷 P3386 【模板】二分图匹配 Dinic版
题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...
- Linux下PortSentry的配置
Linux下PortSentry的配置 前年写过<IDS与IPS功能分析>一文,受到广大读者关注,现将近期有关IDS配置的文章和大家分享. Internet上的服务器一般 ...
- 将二级目录下的文件合并成一个文件的Python小脚本
这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动) #coding:utf8 import sys, os def process(path): new_fi ...
- UML类图与类图中的关系
以下内容摘自<Java与模式> 在类与类之间,会有连线指明它们之间的关系.类和类.类和接口.接口和接口之间可以建立以下几种关系:一般化关系.关联关系.聚合关系.合成关系和依赖关系,这几种关 ...
- C++ 与C# 对应的变量互转
一.C++ 与C# 对应的变量互转的使用实例 C++的动态链接库的函数: C# 调用C++动态链接库数据类型的转换,其中在C++中数据类型为char *,在C#中对应的数据类型为intPtr. 二.常 ...
- 【习题 7-8 UVA-12107】Digit Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大层数.(也即改变的数字个数 然后枚举第一个改哪个数字,第二个改哪个数字.. 一定要注意字典序问题. 每次优先 ...
- nagios 安装配置(包含nrpe端)全 (一)
一.nagios安装: 1.安装下面命令: 这是本人监控服务时自己定义插件所用到的几个系统命令.可不安装. (1)iostat:监控磁盘IO信息: apt-getinstall sysstat (2) ...
- elasticsearch cluster 详解
上一篇通过clusterservice对cluster做了一个简单的概述, 应该能够给大家一个初步认识.本篇将对cluster的代码组成进行详细分析,力求能够对cluster做一个更清晰的描述.clu ...
- 洛谷 P2299 Mzc和体委的争夺战
洛谷 P2299 Mzc和体委的争夺战 题目背景 mzc与djn第四弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙), ...