zoukankan      html  css  js  c++  java
  • The merchant

    The merchant
    Time Limit: 3000MS   Memory Limit: 65536K
         

    Description

    There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path. When he move along a path, he can choose one city to buy some goods and sell them in a city after it. The goods in all cities are the same but the prices are different. Now your task is to calculate the maximum possible profit on each path.

    Input

    The first line contains N, the number of cities.
    Each of the next N lines contains wi the goods' price in each city.
    Each of the next N-1 lines contains labels of two cities, describing a road between the two cities.
    The next line contains Q, the number of paths.
    Each of the next Q lines contains labels of two cities, describing a path. The cities are numbered from 1 to N.

    1 ≤ NwiQ ≤ 50000 

    Output

    The output contains Q lines, each contains the maximum profit of the corresponding path. If no positive profit can be earned, output 0 instead.

    Sample Input

    4
    1 
    5 
    3 
    2
    1 3
    3 2
    3 4
    9
    1 2
    1 3
    1 4
    2 3
    2 1
    2 4
    3 1
    3 2
    3 4

    Sample Output

    4
    2
    2
    0
    0
    0
    0
    2
    0
    分析:考虑只有三种情况,一种是买卖都在起点至LCA,第二种是LCA至终点,第三种是买在起点至LCA,卖在LCA至终点;
       LCA维护区间买卖最大值最小值即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=5e4+10;
    const int N=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,fa[20][maxn],up[20][maxn],go[20][maxn],ma[20][maxn],mi[20][maxn],a[maxn],dep[maxn];
    vi e[maxn];
    void dfs(int x,int y)
    {
        dep[x]=dep[y]+1;
        fa[0][x]=y;
        ma[0][x]=mi[0][x]=a[y];
        for(int i=1;fa[i-1][fa[i-1][x]];i++)
        {
            fa[i][x]=fa[i-1][fa[i-1][x]];
            ma[i][x]=max(ma[i-1][x],ma[i-1][fa[i-1][x]]);
            mi[i][x]=min(mi[i-1][x],mi[i-1][fa[i-1][x]]);
            up[i][x]=max(max(up[i-1][x],up[i-1][fa[i-1][x]]),ma[i-1][fa[i-1][x]]-mi[i-1][x]);
            go[i][x]=max(max(go[i-1][x],go[i-1][fa[i-1][x]]),ma[i-1][x]-mi[i-1][fa[i-1][x]]);
        }
        for(int i=0;i<e[x].size();i++)
        {
            int z=e[x][i];
            if(z==y)continue;
            dfs(z,x);
        }
    }
    int lca(int x,int y)
    {
        if(dep[x]<dep[y])swap(x,y);
        for(int i=19;i>=0;i--)if(dep[fa[i][x]]>=dep[y])x=fa[i][x];
        if(x==y)return x;
        for(int i=19;i>=0;i--)if(fa[i][x]!=fa[i][y])x=fa[i][x],y=fa[i][y];
        return fa[0][x];
    }
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]);
        rep(i,1,n-1)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
        dfs(1,0);
        scanf("%d",&m);
        while(m--)
        {
            int b,c,d;
            scanf("%d%d",&b,&c);
            d=lca(b,c);
            int ret=0,mii=a[b],maa=a[c];
            for(i=19;i>=0;i--)
            {
                if(dep[fa[i][b]]>=dep[d])
                {
                    ret=max(ret,up[i][b]);
                    ret=max(ret,ma[i][b]-mii);
                    mii=min(mii,mi[i][b]);
                    b=fa[i][b];
                }
            }
            for(i=19;i>=0;i--)
            {
                if(dep[fa[i][c]]>=dep[d])
                {
                    ret=max(ret,go[i][c]);
                    ret=max(ret,maa-mi[i][c]);
                    maa=max(maa,ma[i][c]);
                    c=fa[i][c];
                }
            }
            ret=max(ret,maa-mii);
            printf("%d
    ",ret);
        }
        return 0;
    }
  • 相关阅读:
    更改默认alert框体
    自定义垂直拖动的seekbar进度条
    android适配pad和部分手机底部虚拟按键+沉浸式状态栏
    解决studio的URI is not registered (Setting|Language&Frameworks|Schemas and DTDs)
    王者荣耀是怎样炼成的(二)《王者荣耀》unity安装及使用的小白零基础入门
    王者荣耀是怎样炼成的(一)《王者荣耀》用什么开发,游戏入门,unity3D介绍
    使用python(command line)出现的ImportError: No module named 'xxx'问题
    Android Studio生成keystore签名文件步骤讲解
    greendao数据库初次使用的配置及多表关联的初始化
    android视频双向实时通讯的横竖屏切换
  • 原文地址:https://www.cnblogs.com/dyzll/p/6530307.html
Copyright © 2011-2022 走看看