ural 1772

有n个跑道,一开始在s跑道,有k个障碍物,问切换最少的跑道数。

当处理到第i个障碍物的时候,可以从前面没有被遮挡的端点来转移,并且肯定是最近的端点来转移的,用set来维护。

找到转移点后把该障碍物遮挡的点全部给删除掉。

最后找到从哪条跑道上的极小值输出答案。

#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
using namespace std;

const int maxn = 100007;
const long long INF = 50007ll * 50000ll * 50000ll;
set<int> point;
set<int>::iterator pre, last, cnt;
vector<int> point_to_erase;
long long dp[maxn];
int main() {
//    freopen("in.txt", "r", stdin);
    int n, s, k;
    scanf("%d%d%d", &n, &s, &k);
    point.insert(0);
    point.insert(n + 1);
    point.insert(s);
    for (int i = 0; i <= n + 1; i++) {
        dp[i] = INF;
    }
    dp[s] = 0;
    while (k--) {
        int a, b;
        scanf("%d%d", &a, &b);
        if (a > 1) {
            a--;
            point.insert(a);
            cnt = point.find(a);
            int loc_1 = *cnt;
            int loc_2, loc_3;
            pre = --cnt;
            cnt++;
            last = ++cnt;
            loc_2 = *pre;
            loc_3 = *last;
            long long temp = dp[loc_2] + loc_1 - loc_2;
            if (temp < dp[loc_1]) {
                dp[loc_1] = temp;
            }
            temp = dp[loc_3] + loc_3 - loc_1;
            if (temp < dp[loc_1]) {
                dp[loc_1] = temp;
            }
            a++;
        }
        if (b < n) {
            b++;
            point.insert(b);
            cnt = point.find(b);
            int loc_1 = *cnt;
            int loc_2, loc_3;
            pre = --cnt;
            cnt++;
            last = ++cnt;
            loc_2 = *pre;
            loc_3 = *last;
            long long temp = dp[loc_2] + loc_1 - loc_2;
            if (temp < dp[loc_1]) {
                dp[loc_1] = temp;
            }
            temp = dp[loc_3] + loc_3 - loc_1;
            if (temp < dp[loc_1]) {
                dp[loc_1] = temp;
            }
            b--;
        }
        point_to_erase.clear();
        for (cnt = point.lower_bound(a); cnt != point.end() && (*cnt) <= b; cnt++) {
            point_to_erase.push_back(*cnt);
        }
        for (int i = 0; i < point_to_erase.size(); i++) {
            dp[point_to_erase[i]] = INF;
            point.erase(point_to_erase[i]);
        }
    }
    long long ans = INF;
    for (int i = 1; i <= n; i++) {
        if (ans > dp[i]) {
            ans = dp[i];
        }
    }
    printf("%I64d\n", ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值