zoukankan      html  css  js  c++  java
  • HDU6447 网络赛 YJJ's Salesman(DP + 线段树)题解

    思路:若用dp[i][j]表示走到(i,j)的最大值,那么dp[i][j] = max(dp[i - 1][j],dp[i][j - 1],dp[i - 1][j - 1] + v),显然O(n^2)超时。但是我们可以优化这个dp,我们用f[j]表示第i行第j列当前最大值,那么f[j] = max(f[j],f[k] + v[i][j]),k范围0~j - 1,所以我们只要用O(logn)找到f[k]就行了,显然可以用线段树维护f[j]。我们先离散化,然后从上到下,从右到左排序,从右到左是因为我们在更新第i行第j列时,我们所需要的f[k]是第i-1行以前的f[k],这里需要注意。

    代码:

    #include<map>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    using namespace std;
    const int maxn = 100000 + 10;
    const int seed = 131;
    const int MOD = 100013;
    const int INF = 0x3f3f3f3f;
    struct node{
        int x, y, v;
    }q[maxn];
    bool cmp(node a, node b){
        if(a.x < b.x) return true;
        if(a.x == b.x && a.y > b.y) return true;
        return false;
    }
    
    int Max[maxn << 2];
    void update(int l, int r, int pos, int v, int rt){
        if(l == r){
            Max[rt] = max(Max[rt], v);
            return;
        }
        int m = (l + r) >> 1;
        if(pos <= m)
            update(l, m, pos, v, rt << 1);
        else
            update(m + 1, r, pos, v, rt << 1 | 1);
        Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
    }
    int query(int l, int r, int L, int R, int rt){
        if(L <= l && R >= r){
            return Max[rt];
        }
        int MAX = 0;
        int m = (l + r) >> 1;
        if(L <= m)
            MAX = max(MAX, query(l, m, L, R, rt << 1));
        if(R > m)
            MAX = max(MAX, query(m + 1, r, L, R, rt << 1 | 1));
        return MAX;
    }
    int x[maxn], y[maxn], f[maxn];
    int main(){
        int T, n;
        scanf("%d", &T);
        while(T--){
           scanf("%d", &n);
           for(int i = 0; i < n; i++){
                scanf("%d%d%d", &x[i], &y[i], &q[i].v);
                q[i].x = x[i];
                q[i].y = y[i];
           }
           sort(x, x + n);
           sort(y, y + n);
           int cnt1 = unique(x, x + n) - x;
           int cnt2 = unique(y, y + n) - y;
           for(int i = 0; i < n; i++){
                q[i].x = lower_bound(x, x + cnt1, q[i].x) - x + 1;
                q[i].y = lower_bound(y, y + cnt2, q[i].y) - y + 1;
           }
           sort(q, q + n, cmp);
    
           memset(Max, 0, sizeof(Max));
           for(int i = 0; i < n; i++){
               int ret;
               if(q[i].y == 1){
                    ret = q[i].v;
               }
               else{
                    ret = query(1, cnt2, 1, q[i].y - 1, 1) + q[i].v;
               }
               update(1, cnt2, q[i].y, ret, 1);
           }
           printf("%d
    ", Max[1]);
        }
        return 0;
    }
  • 相关阅读:
    mysql联合主键自增、主键最大长度小记
    针对list集合进行分页展示
    初识javascript变量和基本数据类型
    12.19如何定义正则表达式
    人民币符号在html的显示方法
    windows下的NodeJS安装
    css 实现未知图片垂直居中
    IE678下placeholder效果,支持文本框和密码框
    jvm004 解析与分派
    jvm003 类加载的过程
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9540681.html
Copyright © 2011-2022 走看看