题目:Parking Lot

传送门:http://codeforces.com/problemset/problem/46/D

分析:

做法一:

1)这题和Hotel那题一样,也可以看做是求区间空位的问题,不过相对于Hotel那题细节会更多一些。

2)头和尾是不用考虑前和后是否有车的,但可以在$-B$和$L+F$这两个位置假装停了一辆车,这样就不用考虑第一辆插入的车和插入在最后的车啦,减少思考量。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxN=;
int a[],b[];
struct Segment{
int setL,setR,setN;
int l0[maxN<<],r0[maxN<<],m0[maxN<<],tag[maxN<<];
void Down(int v,int l,int r){
if(tag[v]==-)return;
int mid=(l+r)>>;
l0[v<<]=r0[v<<]=m0[v<<]=(!tag[v])*(mid-l+);
l0[v<<^]=r0[v<<^]=m0[v<<^]=(!tag[v])*(r-mid);
tag[v<<]=tag[v<<^]=tag[v];tag[v]=-;
}
void Up(int v,int l,int r){
int mid=(l+r)>>;
if(l0[v<<]==(mid-l+))l0[v]=l0[v<<]+l0[v<<^];else l0[v]=l0[v<<];
if(r0[v<<^]==(r-mid))r0[v]=r0[v<<]+r0[v<<^];else r0[v]=r0[v<<^];
m0[v]=max(max(m0[v<<],m0[v<<^]),r0[v<<]+l0[v<<^]);
}
int que(int v,int l,int r){
if(l==r)return l;
Down(v,l,r);
int mid=(l+r)>>;
if(m0[v<<]>=setN)return que(v<<,l,mid);
if(r0[v<<]+l0[v<<^]>=setN) return mid-r0[v<<]+;
return que(v<<^,mid+,r);
}
void set(int v,int l,int r){
if(setL<=l && r<=setR){
l0[v]=r0[v]=m0[v]=(!setN)*(r-l+);
tag[v]=setN;return;
}
Down(v,l,r);
int mid=(l+r)>>;
if(setL<=mid)set(v<<,l,mid);
if(mid< setR)set(v<<^,mid+,r);
Up(v,l,r);
}
}T;
int main(){
int L,B,F,n;
scanf("%d%d%d%d",&L,&B,&F,&n);
T.l0[]=T.r0[]=T.m0[]=B+L+F;
for(int op,x,d,td,i=;i<=n;++i){
scanf("%d",&op);
if(op==){
scanf("%d",&td);d=B+td+F;
if(T.m0[]<d)puts("-1");
else{
T.setN=d;x=T.que(,-B,L+F-);
a[i]=T.setL=x+B;b[i]=T.setR=a[i]+td-;T.setN=;
T.set(,-B,L+F-);
printf("%d\n",a[i]);
}
}else{
scanf("%d",&td);
T.setL=a[td];T.setR=b[td];T.setN=;
T.set(,-B,L+F-);
}
}
return ;
}

做法二:

2)关注到$n$非常小

3)利用map来储存停了车区间,保留区间头尾就好了

4)插入车:遍历map,查看两个停车区间的中间是否可以停下当前这辆车

代码:

#include <bits/stdc++.h>
using namespace std;
map<int,int>mp;
int a[];
int main(){
int L,B,F,n;
scanf("%d%d%d%d",&L,&B,&F,&n);
mp[-B]=-B;mp[L+F]=L+F;
for(int i=,op,x;i<=n;++i){
scanf("%d%d",&op,&x);
if(op==){
a[i]=-;
for(auto it1=mp.begin(),it2=++mp.begin();it2!=mp.end();++it1,++it2) {
if(it2->first - it1->second >=B+x+F) {
a[i]=it1->second + B;
mp[a[i]]=a[i]+x;
break;
}
}
printf("%d\n",a[i]);
}else mp.erase(a[x]);
}
return ;
}

[CF46D]Parking Lot的更多相关文章

  1. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  2. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  3. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  4. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  5. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

  6. [LintCode] Parking Lot 停车场问题

    Design a parking lot. see CC150 OO Design for details.1) n levels, each level has m rows of spots an ...

  7. [CareerCup] 8.4 Parking Lot 停车场问题

    8.4 Design a parking lot using object-oriented principles. LintCode上的原题,请参见我的另一篇博客Parking Lot 停车场问题. ...

  8. Codeforces 46D Parking Lot

    传送门 D. Parking Lot time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. 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 ...

随机推荐

  1. mysql日志信息查看与设置mysql-bin

    查看 sql查询记录  日志是否开启 SHOW GLOBAL VARIABLES LIKE '%general_log%' 二进制日志 是否开启 SHOW GLOBAL VARIABLES LIKE ...

  2. 安装Linux系统CentOS6.5

    个人机器搭建分布式环境时避免要使用虚拟机来满足分布式环境所需的机器,当然伪分布式除外. 简单记录下虚拟机环境的创建过程,Mac上常用的虚拟机VMware Fusion. 虚拟机资源库中新建虚拟机: 选 ...

  3. The order of a Tree

    The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...

  4. POJ 3549 GSM phone(圆+扫描线+最短路)

    题目意思是求起点s到终点s的最短路,但是只能在圆的内部和边上走.一种可以想到的方法就是求出所有的交点,然后两两连边并验证合法性,但是这样的交点数规模有n2. 我们可以观察发现,我们在圆求并构成的图形中 ...

  5. so easy(并查集+unordered_map)

    There are nn points in an array with index from 11 to nn, and there are two operations to those poin ...

  6. Sql 字符串自增列的实现

    ALTER FUNCTION [dbo].[f_NextID](@tabname VARCHAR()) RETURNS ) AS BEGIN DECLARE @charval CHAR() IF LO ...

  7. js中的数组去掉空值

    //result 是有空值的数组//r是处理好的数组var r = result.filter(function (s) { return s && s.trim();});

  8. FreeMarker三宗罪之优缺点

    FreeMarker是Quake Wang推荐我使用的.刚学FreeMarker的时候,发现freemarker真的很棒!简单易用,功能强大.但是用它做了几个项目以后开始不爽了. 一宗罪:freema ...

  9. XIB约束布局问题(button)

    button默认不给宽度:系统Xib自动适配,最小宽度30.在使用宽度计算时,无法小于这个值

  10. Django之cookie 和session

    ---恢复内容开始--- 一.cookie 前戏.cookie 的由来 由于http协议是无状态的 无法记录用户状态 cookie就是保存在客户端浏览器上的键值对 工作原理:当你登陆成功之后 浏览器会 ...