zoukankan      html  css  js  c++  java
  • YJJ's Salesman

    YJJ's Salesman

    YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. 
    One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0(0,0) on the rectangle map and B (109,109)(109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y)(x,y) now (0x109,0y109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1). 
    On the rectangle map from (0,0)(0,0) to (109,109)(109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village kk on (xk,yk)(xk,yk) (1xk109,1yk109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk1,yk1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk dollars.(YJJ may get different number of dollars from different village.) 
    YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

    线段树+区间离散化+dp

    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    typedef long long ll;
    #define P pair<ll,ll>
    #define sc(x) scanf("%I64d",&x);
    #define maxn 100005
    struct Nod
    {
        int x,y,v;
    
    };
    Nod A[maxn];
    int N;
    int L[maxn*4],R[maxn*4],V[maxn*4];
    bool cmp(Nod a,Nod b)
    {
        if(a.x==b.x)return a.y>b.y;
        return a.x<b.x;
    }
    void build(int l,int r,int x)
    {
    
        L[x]=l;
        R[x]=r;
    
        if(l==r)
        {
            V[x]=0;
            return ;
        }
        int mid=(l+r)/2;
        build(l,mid,2*x);
        build(mid+1,r,2*x+1);
        V[x] =0;
    }
    int query(int l,int r,int x)
    {
        if(r==0)return 0;
        if(L[x]>=l&&R[x]<=r)
        {
            return V[x];
    
        }
        else
        {
            int mid=(L[x]+R[x])/2;
            if(r<=mid)return query(l,r,2*x);
            else if(l>mid)return query(l,r,2*x+1);
            else return max(query(l,r,2*x),query(l,r,2*x+1));
    
        }
    }
    void update(int x,int pos,int w)
    {
        if(L[x]==R[x])
        {
            V[x]=max(w,V[x]);
            return;
        }
        int mid=(L[x]+R[x])/2;
        if(mid>=pos)update(x*2,pos,w);
        else update(x*2+1,pos,w);
        V[x]=max(V[2*x],V[2*x+1]);
    
    
    }
    int B[maxn];
    signed main()
    {
        int T;
        sc(T);
        while(T--)
        {
            sc(N);
            for(int i=1; i<=N; i++)
            {
                sc(A[i].x);
                sc(A[i].y);
                sc(A[i].v);
                B[i]=A[i].y;
            }
            sort(B+1,B+N+1);
            int siz=unique(B+1,B+N+1)-B-1;
            for(int i=1; i<=N; i++)
            {
                A[i].y=lower_bound(B+1,B+siz+1,A[i].y)-B;
            }
            build(1,N,1);
            sort(A+1,A+N+1,cmp);
            int ans=0;
            for(int i=1; i<=N; i++)
            {
                ll t=query(1,A[i].y-1,1)+A[i].v;
                // cout<<A[i].y-1<<" "<<t<<'
    ';
                update(1,A[i].y,t);
                ans=max(ans,t);
            }
            cout<<ans<<'
    ';
        }
    }

     树状数组大法好

    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    typedef long long ll;
    #define P pair<ll,ll>
    #define sc(x) scanf("%I64d",&x);
    #define maxn 100005
    struct Nod
    {
        int x,y,v;
    
    };
    Nod A[maxn];
    int N;
    int V[maxn*4];
    bool cmp(Nod a,Nod b)
    {
        if(a.x==b.x)return a.y>b.y;
        return a.x<b.x;
    }
    void add(int x,int val)
    {
        while(x<=N){
            V[x]=max(val,V[x]);
            x+=(x&-x);
        }
    }
    int get(int x)
    {
        if(x==0)return 0;
        int ans=0;
        while(x){
            ans=max(V[x],ans);
            x-=(x&-x);
        }
        return ans;
    }
    int B[maxn];
    signed main()
    {
        int T;
        sc(T);
        while(T--)
        {
            memset(V,0,sizeof V);
            sc(N);
            for(int i=1; i<=N; i++)
            {
                sc(A[i].x);
                sc(A[i].y);
                sc(A[i].v);
                B[i]=A[i].y;
            }
            sort(B+1,B+N+1);
            int siz=unique(B+1,B+N+1)-B-1;
            for(int i=1; i<=N; i++)
            {
                A[i].y=lower_bound(B+1,B+siz+1,A[i].y)-B;
            }
            //build(1,N,1);
            sort(A+1,A+N+1,cmp);
            int ans=0;
            for(int i=1; i<=N; i++)
            {
                ll t=get(A[i].y-1)+A[i].v;
               //  cout<<A[i].y-1<<" "<<t<<'
    ';
                add(A[i].y,t);
                ans=max(ans,t);
            }
            cout<<ans<<'
    ';
        }
    }
  • 相关阅读:
    ecshop首页最新评论的调用
    在ECSHOP商品列表页显示每个商品的评论等级和评论数量
    ecshop 系统信息在哪个页面
    ECSHOP去版权_ECSHOP2.7.2去版权方法最新方法
    ECShop 自定义函数以及调用
    ecshop 首页如何调用积分商城里面的的商品
    回到顶部的js代码
    ./flow.php (购物流程)
    C#把字符串转时间格式
    如何将服务端的多个文件打包下载(转)
  • 原文地址:https://www.cnblogs.com/liulex/p/11391793.html
Copyright © 2011-2022 走看看