zoukankan      html  css  js  c++  java
  • POJ

    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;
    }
    

      

  • 相关阅读:
    素数回文 ---- 有点暴力.....
    Manacher算法 , 实例 详解 . NYOJ 最长回文
    大数处理 详解 模版
    River Crossing 简单的动态规划 .
    hdu
    产生冠军 map 的 应用 .
    MySQL的数据库备份与恢复。
    rsync的相关使用,参数设置。
    centos与mac安装python虚拟环境 virtualenvwrapper
    CentOS7安装Python3.7
  • 原文地址:https://www.cnblogs.com/visualVK/p/9719346.html
Copyright © 2011-2022 走看看