POJ - 1661 Help Jimmy

input:

1
3 8 17 20
0 10 8
0 10 13
4 14 3

  

output:

23

  

分析:

dp。以从左下方上来为例,dp[i][0]=第i块阶梯从离i最近且合法的左下方阶梯过来的最短时间
dp[i][0]=block[i].h-block[k].h+min(dp[k][0]+block[i].l-block[k].l,dp[k][1]+block[k].r-block[i].l)
右下方同理。

  

code:

#define frp

//#include<bits/stdc++.h>
#include<cmath>
#include<algorithm>
#include<iostream>

using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 1000;
const int MAXN = 20050;

struct node {
    int l, r, h;

    node(int x1 = 0, int x2 = 0, int height = 0) : l(x1), r(x2), h(height) {}

    bool operator<(const node &a) const {
        return h < a.h;
    }
} block[MAXN];

int n, x, y, MAXH;
int dp[MAXN][2];

bool inside(int x, int l, int r) {
    return x >= l && x <= r;
}

void turnLeftOrRight(int i, int j) {
    int k = i - 1;
    while (k > 0 && block[i].h - block[k].h <= MAXH) {
        if (j == 0 &&inside(block[i].l,block[k].l,block[k].r)) {
            dp[i][0] = block[i].h - block[k].h +
                       min(dp[k][0] + block[i].l - block[k].l, dp[k][1] + block[k].r - block[i].l);
            return;
        } else if (j == 1 &&inside(block[i].r,block[k].l,block[k].r)) {
            dp[i][1] = block[i].h - block[k].h +
                       min(dp[k][0] + block[i].r - block[k].l, dp[k][1] + block[k].r - block[i].r);
            return;
        }
        k--;
    }
    if (block[i].h - block[k].h <= MAXH) {
        dp[i][j] = block[i].h;
    } else {
        dp[i][j] = inf;
    }
}

void solve() {
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> x >> y >> MAXH;
        block[0] = node(x, x, y);
        for (int i = 1; i < n + 1; i++) {
            int l, r, h;
            cin >> l >> r >> h;
            block[i] = node(l, r, h);
        }
        block[n + 1] = node(-inf, inf, 0);
        sort(block, block + n + 2);

        for (int i = 1; i <= n + 1; i++) {
            turnLeftOrRight(i, 0);
            turnLeftOrRight(i, 1);
        }
        cout << min(dp[n + 1][0], dp[n + 1][1]) << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef frp
    freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
//    freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}

  

转载于:https://www.cnblogs.com/visualVK/p/9719346.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值