CF 46D Parking Lot(SET)

转自:http://www.cnblogs.com/wuyiqi/archive/2012/05/01/2477735.html

题目链接:http://codeforces.com/problemset/problem/46/D


题目大意:

在长为L的(点0~点L)的线段上停车开车操作;

停车 1 x:车长x,需要b+x+f的空余才能使车停进去,多个区域,选择最左边

开车 2 x:第x次操作,停进的车,开出去

分析:因为需要插入的次数很小,所以可以直接记录,每辆汽车一次插入的位置,被占据的位置和空位间肯定是一段一段相邻的

即,比如有空位 a-b、c-d,       b-c  被占据了,如果a-b不够长,那么指针就要往右移动两个位置了,即跳过中间被占据的位置

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int L, B, F;
int pos[105][2], tm=0;
set<int> s;
int main(void) {
    scanf("%d%d%d", &L, &B, &F);
    s.insert(-B);
    s.insert(L+F);
    int n;
    scanf("%d", &n);
    while(n--) {
        int x, y;
        scanf("%d%d", &x, &y);
        ++tm;
        if (x == 1) {
            int f=0;
            for(set<int>::iterator it=s.begin(); it!=s.end(); ) {
                set<int>::iterator nxt=it;
                ++nxt;
                if (nxt!=s.end() && ((*it)+B+y+F <= *nxt)) {
                    printf("%d\n", (*it)+B);
                    s.insert((*it)+B);
                    s.insert((*it)+B+y);
                    pos[tm][0] = *it+B;
                    pos[tm][1] = *it+B+y;
                    f=1;
                    break;
                }
                ++it; if(it!=s.end()) ++it;
            }
            if(!f) puts("-1");
        } else {
            s.erase(pos[y][0]);
            s.erase(pos[y][1]);
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值