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;
    }
  • 相关阅读:
    Python 面向对象 —— super 的使用(Python 2.x vs Python 3.x)
    安全移除驱动器、弹出、卸载的差别及详细查看设备的运行前后的异同
    java中不常见的keyword:strictfp,transient
    textarea文本域宽度和高度(width、height)自己主动适应变化处理
    Android 输入框弹出样式
    .net下载优酷1080P视频
    Oracle Hints具体解释
    关于成本核算方法、步骤、成本分析的简单回复
    程序猿接私活经验总结,来自csdn论坛语录
    Android getResources的作用和须要注意点
  • 原文地址:https://www.cnblogs.com/dyzll/p/6530307.html
Copyright © 2011-2022 走看看