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 ≤ 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
分析:考虑只有三种情况,一种是买卖都在起点至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+;
const int N=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,fa[][maxn],up[][maxn],go[][maxn],ma[][maxn],mi[][maxn],a[maxn],dep[maxn];
vi e[maxn];
void dfs(int x,int y)
{
dep[x]=dep[y]+;
fa[][x]=y;
ma[][x]=mi[][x]=a[y];
for(int i=;fa[i-][fa[i-][x]];i++)
{
fa[i][x]=fa[i-][fa[i-][x]];
ma[i][x]=max(ma[i-][x],ma[i-][fa[i-][x]]);
mi[i][x]=min(mi[i-][x],mi[i-][fa[i-][x]]);
up[i][x]=max(max(up[i-][x],up[i-][fa[i-][x]]),ma[i-][fa[i-][x]]-mi[i-][x]);
go[i][x]=max(max(go[i-][x],go[i-][fa[i-][x]]),ma[i-][x]-mi[i-][fa[i-][x]]);
}
for(int i=;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=;i>=;i--)if(dep[fa[i][x]]>=dep[y])x=fa[i][x];
if(x==y)return x;
for(int i=;i>=;i--)if(fa[i][x]!=fa[i][y])x=fa[i][x],y=fa[i][y];
return fa[][x];
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n-)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
dfs(,);
scanf("%d",&m);
while(m--)
{
int b,c,d;
scanf("%d%d",&b,&c);
d=lca(b,c);
int ret=,mii=a[b],maa=a[c];
for(i=;i>=;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=;i>=;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\n",ret);
}
return ;
}
The merchant的更多相关文章
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
- POJ 3278 The merchant
传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there i ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...
- ThoughtWorks Merchant's Guide To The Galaxy
ThoughtWorks笔试题之Merchant's Guide To The Galaxy解析 一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天H ...
- [POJ 3728]The merchant
Description There are N cities in a country, and there is one and only one simple path between each ...
- opencart3图片Google Merchant Center验证通过不了的解决方法
最近在做一个opencart项目,有对接Google Merchant Center,但是一直提示产品图片验证无法通过,ytkah看了一下图片路径,/image/cache/catalog/demo/ ...
- 如何查看卖家ID (Merchant ID) 亚马逊哪里找?
如何查看卖家ID (Merchant ID) 亚马逊哪里找? 如何查看卖家ID (Merchant ID) 亚马逊哪里找? 1. 找到想要获取ID的卖家,点击店铺名(跟卖的卖家会收在”Other Se ...
- poj3728The merchant 【倍增】【LCA】
There are N cities in a country, and there is one and only one simple path between each pair of citi ...
- POJ 3728 The merchant(LCA+DP)
The merchant Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- 雅礼集训 Day6 T1 Merchant 解题报告
Merchant 题目描述 有\(n\)个物品,第\(i\)个物品有两个属性\(k_i,b_i\),表示它在时刻\(x\)的价值为\(k_i\times x+b_i\). 当前处于时刻\(0\),你可 ...
随机推荐
- hibernate字段名和属性
字段名和属性名相同 Annotation:默认为@Basic 注意:如果在成员属性没有加入任何注解,则默认在前面加入了@Basic Xml中不用写column 字段名和属性名不同 Annotation ...
- kubernetes Traefik ingress配置详解
理解Ingress 简单的说,ingress就是从kubernetes集群外访问集群的入口,将用户的URL请求转发到不同的service上.Ingress相当于nginx.apache等负载均衡方向代 ...
- shell脚本-数组
shell脚本-数组 数组 变量:存储单个元素的内存空间. 数组:存储多个元素的连续的内存空间,相当于多个变量的集合. 数组索引:编号从0开始,属于数值索引.索引可支持使用自定义的格式,而不仅是数值格 ...
- 删除".SVN"文件夹方法(转载)
转自:http://www.cnblogs.com/lr-ting/archive/2012/09/03/2666271.html 一.在linux下 删除这些目录是很简单的,命令如下 find . ...
- [Swift通天遁地]六、智能布局-(1)给视图添加尺寸和中心点的约束
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- $CF1141B Maximal Continuous Rest$
告诉你一天n件事情 a[i]为1是休息 a[i]为2是工作 求最长连续的休息时间(即最长的1 可以作为环状来求.(即环状最长的1 这题就可以用前缀和贪心等什么操作.. 然后用\(ans1ans2\)瞎 ...
- nginx入门学习
1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了 yum install gcc patch libffi-devel python-devel zlib-devel bzip ...
- ACM_堆箱子咯(栈)
堆箱子咯 Time Limit: 2000/1000ms (Java/Others) Problem Description: 双十一大家都在买买买,可忙坏了快递小哥了.zl和皮卡鸡在大伙在剁手的时候 ...
- 研磨JavaScript系列(二):没有类
object就是对象的类型.在JavaScript中不管多么复杂的数据和代码.都可以组织成object形式的对象. 但JavaScript没有"类"概念. 看下面这段JavaScr ...
- 国内外知名IT科技博客
国内 1.36氪(www.36kr.com): 目前国内做的最风生水起的科技博客,以介绍国内外互联网创业新闻为主的博客网站,自己建立有36Tree互联网创业融投资社区.36氪的名字源于元素周期 表的第 ...