码迷,mamicode.com
首页 > 其他好文 > 详细

poj——3728 The merchant

时间:2017-08-23 18:22:31      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:number   ret   getchar   output   count   sam   plm   问题   include   

                          The merchant

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5055   Accepted: 1740

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

Source

题目大意:给一棵N个点的树,每个点上都买东西,给你每个点上的价格,现在有一个商人,当他从一个点出发走到另一个点的时候(因为是树,路径唯一),在路上有一次机会在一个点上买一个东西,而在后面的一个点上卖掉来挣钱,当然他也可以不要这一次机会。现在的问题是,给出M个询问,每个询问一个起点,一个终点,你来回答路上最多能挣多少钱。

思路:(天哪,这是我见过最恶心的lca!没有之一!!!)

 我们维护4个值,一是到当前点的最大值,二是到当前点的最小值,三是从上往下(从父亲到儿子)的最大利润,四是从下往上(从儿子到父亲)的最大利润。

然后我们进行倍增更新这四个值。由于我们是一棵树,所以我们可以用当前点的儿子和他本身来更新这几个值

也就是这样:  shop[x][i].maxx=max(shop[x][i-1].maxx,shop[shop[x][i-1].dad][i-1].maxx);
        shop[x][i].minn=min(shop[x][i-1].minn,shop[shop[x][i-1].dad][i-1].minn);

有人会问不是说要用他自己和他的儿子来更新吗?!那shop[shop[x][i-1].dad][i-1].minn这又是个什么鬼?!

这个地方我也是纠结了好长时间,我们更新的时候有一句话是这样的:shop[x][i].dad=shop[shop[x][i-1].dad][i-1].dad;这样的话也就是说这两个点是相等的。为什么?!我们来看shop[x][i].dad这里是x的第2^i个父亲,后面的是它的第2^(i-1)的第2^(i-1)个父亲,我们可以代入数据试一下,这两个值是相等的。例如:当i=1的时候她的第二个父亲不就是它的父亲的父亲吗。。。当i=2的时候,2^2=(2^1)+(2^1)  当i=3时,2^3=(2^2)+(2^2)   当i=4 2^4=2^3+2^3………………这样我们就可以很好地理解为什么会是这样了吧、、、是不是又在想那直接更新不就行了,让过来绕过去不还是用它自己更新吗、、、可是我们知道我们是挨着更新的,i-1是更新过的但是i就不保证是更新过的了。。。。

然后我们再求lca,为什么要求lca?!

啊啊啊,你去一个地方肯定是走最近的路了,那不就是一定会走lca吗、、、(除非你闲的没事干、、、)

哼,有位大佬跟我说,求lca是因为他没法倒着走,如果走到比LCA深度还深的点,就到不了目的地了、、、、、、

 

所以我们的路径分为两种情况一个是往上走的另一个是往下走的,我们给出的数据保证第一个数是往上走,第二个数可能是lca也可能往下走。

这样我们就可以明白为什么我们要记录从上往下(从父亲到儿子)的最大利润,从下往上(从儿子到父亲)的最大利润了。

我们在进行得到结果时会有这三种买卖的情况,一是我们在x到lca这进行交易完毕,二是我们在lca到y才进行交易,三是我们在x到lca处买入在lca到y处卖出、、、所以我们对于每一组询问,更新从x到lca的最小值,更新从lca到y的最大值,然后在最后经行比较,同样我们需要更新从上到下可获得最大利润以及从下到上可获得最小利润,这个地方我们采用倍增。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 200010
#define maxn 9999999
using namespace std;
int n,m,x,y,tot,v[N],head[N],deep[N];
struct Edge
{
    int to,from,next;
}edge[N<<1];
struct Shop
{
    int dad,minn,maxx,up_down,down_up;
}shop[N][25];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();}
    return x*f;
}
int dfs(int x,int fa)
{
    deep[x]=deep[fa]+1;
    shop[x][0].maxx=max(v[x],v[fa]);
    shop[x][0].minn=min(v[x],v[fa]);
    shop[x][0].up_down=max(0,v[x]-v[fa]);
    shop[x][0].down_up=max(0,v[fa]-v[x]);
    for(int i=1;shop[x][i-1].dad;i++)
    {
        shop[x][i].dad=shop[shop[x][i-1].dad][i-1].dad;
        shop[x][i].maxx=max(shop[x][i-1].maxx,shop[shop[x][i-1].dad][i-1].maxx);
        shop[x][i].minn=min(shop[x][i-1].minn,shop[shop[x][i-1].dad][i-1].minn);
        shop[x][i].down_up=max(shop[x][i-1].down_up,shop[shop[x][i-1].dad][i-1].down_up);
        shop[x][i].down_up=max(shop[x][i].down_up,shop[shop[x][i-1].dad][i-1].maxx-shop[x][i-1].minn);
        shop[x][i].up_down=max(shop[x][i-1].up_down,shop[shop[x][i-1].dad][i-1].up_down);
        shop[x][i].up_down=max(shop[x][i].up_down,shop[x][i-1].maxx-shop[shop[x][i-1].dad][i-1].minn);
    }
    for(int i=head[x];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(shop[x][0].dad!=t)
         shop[t][0].dad=x,dfs(t,x);
    }
}
int Lca(int x,int y)
{
    if(deep[x]>deep[y]) swap(x,y);
    for(int i=20;i>=0;i--)
     if(deep[shop[y][i].dad]>=deep[x])
      y=shop[y][i].dad;
    if(x==y) return x;
    for(int i=20;i>=0;i--)
     if(shop[x][i].dad!=shop[y][i].dad)
      x=shop[x][i].dad,y=shop[y][i].dad;
    return shop[x][0].dad;
}
int ask(int x,int y)
{
    int ans=0,maxx=-maxn,minn=maxn,lca=Lca(x,y);
    for(int i=20;i>=0;i--)
     if(deep[shop[x][i].dad]>=deep[lca])
       {
             ans=max(ans,max(shop[x][i].down_up,shop[x][i].maxx-minn));
             minn=min(shop[x][i].minn,minn);
             x=shop[x][i].dad;
      }
    for(int i=20;i>=0;i--)
     if(deep[shop[y][i].dad]>=deep[lca])
      {
          ans=max(ans,max(shop[y][i].up_down,maxx-shop[y][i].minn));
          maxx=max(shop[y][i].maxx,maxx);
          y=shop[y][i].dad;
      }
    return max(ans,maxx-minn);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++) v[i]=read();
    for(int i=1;i<n;i++) x=read(),y=read(),add(x,y),add(y,x);
    dfs(1,0);
    m=read();
    for(int i=1;i<=m;i++)
    {
        x=read(),y=read();
        if(x==y) printf("0\n");
        printf("%d\n",ask(x,y));
    }
    return 0;
}

mdzz这个题居然因为一个数组然后调了一下午?!

什么嘛,倍增的时候倍增的数一定要比数组的第二维小!!!

poj——3728 The merchant

标签:number   ret   getchar   output   count   sam   plm   问题   include   

原文地址:http://www.cnblogs.com/z360/p/7412053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!