• poj3728The merchant


                                                                                                                                The merchant

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 4800   Accepted: 1666

    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 ≤ N, wi, Q ≤ 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
    

    Source

    大致翻译:有n个城市,n-1条边,现在一个商人要去买卖东西,每个城市都有固定的价格,现在有q条路径,问你每条路径上买卖能赚到的最多的钱分别是多少?一条路径只能顺着走,不能反过来.
    分析:这道题巨坑爹整整调了一个下午TAT.回到正题,其实题目给我们n个城市,n-1条边就相当于给了我们一棵树,既然是告诉了树上两个点,我们肯定是要把LCA求出来才能知道两个点之间的路径的。求LCA可以利用倍增,那么接下来就要维护一些信息才能完成此题。
         假设有一个询问是(u,v),u,v的LCA为t,那么我们可能在u到t的这段路径买入,在t到v的这段路径卖出,也可能就在u到t的这段路径买入卖出,还可能在t到v的这段路径买入卖出.考虑第一种情况,我们需要记录u到t的最小值和t到v的最大值,考虑第二种情况和第三种情况,我们只需要分别维护这两段路径上的最大收益即可,但是注意从t到v的这段路径,我们倍增总不能从t跳到v吧,我们让u和v往上跳,那么考虑反过程,我们让从v到t的这段路径在价格最高点买入,在价格最低点卖出,统计答案的时候取相反数即可。
         上面说明了我们要维护哪些信息,下面来说明该怎么维护这些信息:
         设minn[i][j]为i到i的第2^j个祖先的最小值,maxx[i][j]为为i到i的第2^j个祖先的最大值,lmax[i][j]为i到i的第2^j个祖先的最大收益,rmin[i][j]为i到i的第2^j个祖先的最小收益(刚刚考虑的特殊情况),fa[i][j]为i的第2^j个祖先。
         可以先利用dfs求出fa[i][0]和每个节点的深度d[i],然后发现:我们可以在维护fa[i][j]的同时维护其他信息:minn[i][j] = min(minn[i][j-1],minn[fa[i][j-1]][j-1]),maxx类同,那么lmax就是最大卖出价-最小买入价,或者是它的子区间中的最大值,即lmax[i][j] = max(max(lmax[i][j - 1], lmax[fa[i][j - 1]][j - 1]), maxx[fa[i][j-1]][j - 1] - minn[i][j - 1]),rmin类同,顺序不能反!
         然后我们需要在倍增的时候处理这些信息。以求从u到t的这段路径为例,维护最小值,最大收益,怎么维护最大收益呢?利用之前的lmax数组和maxx数组和当前维护的最小值,然后维护从t到u的信息。最后的答案可能有3种:从u到t的最大收益,从t到v的最小收益的相反数,从t到v的最大值减去从u到t的最小值,取这3种可能的答案的最大值即可。
    还有一些小细节,可以在代码中查看:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n,a[50010],head[50010],to[100020],nextt[100020],tot,q,d[50010];
    int fa[50010][20], minn[50010][20], maxx[50010][20], lmax[50010][20], rmin[50010][20];
    
    void add(int x, int y)
    {
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    }
    
    void dfs(int u, int from,int dist)
    {
        d[u] = dist;
        fa[u][0] = from;
        for (int i = head[u]; i != -1; i = nextt[i])
        {
            int v = to[i];
            if (from != v)
                dfs(v, u,dist + 1);
        }
    }
    
    void init()
    {
        dfs(1, 0, 0);
        minn[1][0] = maxx[1][0] = a[1];
        lmax[1][0] = -1 << 30;
        rmin[1][0] = 1 << 30;
        for (int i = 2; i <= n; i++)
        {
            minn[i][0] = min(a[i], a[fa[i][0]]);
            maxx[i][0] = max(a[i], a[fa[i][0]]);
            lmax[i][0] = max(a[fa[i][0]] - a[i], 0);
            rmin[i][0] = min(a[fa[i][0]] - a[i], 0);
        }
        for (int j = 1; (1 << j) < n; j++)
            for (int i = 1; i <= n; i++)
            {
            fa[i][j] = fa[fa[i][j - 1]][j - 1];
            maxx[i][j] = max(maxx[i][j - 1], maxx[fa[i][j - 1]][j - 1]);
            minn[i][j] = min(minn[i][j - 1], minn[fa[i][j - 1]][j - 1]);
            lmax[i][j] = max(max(lmax[i][j - 1], lmax[fa[i][j - 1]][j - 1]), maxx[fa[i][j-1]][j - 1] - minn[i][j - 1]);
            rmin[i][j] = min(min(rmin[i][j - 1], rmin[fa[i][j - 1]][j - 1]), minn[fa[i][j - 1]][j - 1] - maxx[i][j - 1]);
            }
    }
    
    int LCA(int x, int y)
    {
        int maxxx = 0, miny = 0, minx = a[x], maxy = a[y];
        for (int i = 16; i >= 0 && d[x] != d[y]; i--)
        {
            if (abs(d[x] - d[y]) >= 1 << i)
            {
                if (d[y] < d[x])
                {
                    maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx);
                    minx = min(minx, minn[x][i]);
                    x = fa[x][i];
                }
                else
                {
                    miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy);
                    maxy = max(maxy, maxx[y][i]);
                    y = fa[y][i];
                }
            }
        }
        if (x == y)
            return max(max(maxxx, -miny), maxy - minx);
        for (int i = 16; i >= 0;i--)
            if (fa[x][i] != fa[y][i] && fa[x][i] && fa[y][i])
            {
            maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx);
            minx = min(minx, minn[x][i]);
            x = fa[x][i];
    
            miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy);
            maxy = max(maxy, maxx[y][i]);
            y = fa[y][i];
            }
        maxxx = max(max(maxxx, lmax[x][0]), maxx[x][0] - minx);
        minx = min(minx, minn[x][0]);
        miny = min(min(miny, rmin[y][0]), minn[y][0] - maxy);
        maxy = max(maxy, maxx[y][0]);
        return max(max(maxxx, -miny), maxy - minx);
        
    }
    
    int main()
    {
        memset(head, -1, sizeof(head));
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for (int i = 1; i <= n - 1; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
        }
        init();
        scanf("%d", &q);
        while (q--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            printf("%d
    ", LCA(x, y));
        }
    
        return 0;
    }
  • 相关阅读:
    学习记录6
    学习记录5
    学习记录4
    学习记录3&开启新的世界
    学习记录2
    学习记录1
    类、对象和包
    班会总结
    请简单描述一下,java程序的运行过程,以及java为什么能跨平台
    JDK、JRE、JVM的区别与关系
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7089371.html
Copyright © 2020-2023  润新知