Codeforces 46D Parking Lot 枚举

题目

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

题目来源:某人总结的线段树合集

简要题意:要停一些车子,车子有长度,每个车子至少要距离前后的车子某距离,可以移走车子,求每次停车的位置。

题解

虽然这题是在一个线段树教程里的,不过数据范围是可以更暴力的。

作为一个懒人,我就暴力了一发。

维护俩数组分别是向前最近后向后最近的车子的位置。

每次查找这个数组去找到符合的位置,然后进行更新。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 1e5+5;

int l[N], r[N];
bool a[N];
PII q[N];

void update(int n) {
    for (int i = 1; i <= n; i++) {
        l[i] = a[i] ? i : l[i-1];
    }
    for (int i = n; i > 0; i--) {
        r[i] = a[i] ? i : r[i+1];
    }
}

int query(int n, int x, int b, int f) {
    for (int i = 1; i <= n; i++) {
        if (i - l[i] > f && r[i] - i >= x + b && x + i - 1 <= n) {
            for (int j = 0; j < x; j++) {
                a[i+j] = true;
            }
            return i;
        }
    }
    return 0;
}

int main() {
    int len, b, f, n, op, x;
    scanf("%d%d%d%d", &len, &f, &b, &n);

    l[0] = -1e9;
    r[len+1] = 1e9;
    update(len);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &op, &x);
        if (op == 1) {
            int ans = query(len, x, b, f);
            q[i] = make_pair(ans, ans + x - 1);
            printf("%d\n", ans-1);
        } else {
            for (int j = q[x].fi; j <= q[x].se; j++) {
                a[j] = false;
            }
        }
        update(len);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值