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<<'
    ';
        }
    }
  • 相关阅读:
    JuiceSSH:安卓平台免费好用的 SSH 客户端
    git&github-本地库推送到远程库
    git&githib-给远程库取别名
    Git分支管理的本质
    MySQL学习笔记(一)--逻辑架构学习
    mysql-主从备份问题小结
    Docker--数据管理之Volumes
    初识OpenSSH--1
    一个最简单的Dockfile实践
    构词法2
  • 原文地址:https://www.cnblogs.com/liulex/p/11391793.html
Copyright © 2011-2022 走看看