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\),你可 ...
随机推荐
- codeforces 764D
脑洞 很早以前没有补掉的题目 四色问题肯定使有解的,然后就是怎么构造.注意到边长是奇数,那么我们就可以分类,按左上角坐标的奇偶性分类,正好对应四种颜色.因为当两个矩形左上角横纵坐标奇偶性不同时,那么肯 ...
- 使用GitHub(转载)
转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137628548491 ...
- php获得文件的属性
PHP获取文件属性可以用到多种函数,来实现我们对文件各种不同信息的获取需求.在这里我们就简单的介绍了这些获取方式的实现方法. 详细解读PHP获取远程图片技巧 详细介绍PHP读取目录函数 如何运用相关函 ...
- hdu2029
http://acm.hdu.edu.cn/showproblem.php?pid=2029 #include<stdio.h> #include<string.h> #inc ...
- C++ friend关键字
友元:友元函数 友元类. 友元函数:不属于任何类,只是在类中声明一下(可以放在 private 或者 public,没有区别),告诉这个类,这个函数是你的朋友,当然朋友不是白当的:这个函数可以访问你的 ...
- [转]linux uniq 命令详解
转自:http://blog.csdn.net/tianmohust/article/details/6997683 uniq 命令 文字 uniq 是LINUX命令 用途 报告或删除文件中重复的 ...
- Linux查找目录下的按时间过滤的文件
在维护项目中,有时会指定都一些条件进行过滤文件,并对该批文件进行操作:这时我们将使用shell命令进行操作:直接上代码 #!/bin/sh #BEGIN #`find ./ ! -name " ...
- Mongodb——文档数据库
mongodb是一个文档数据库. mongo操作 多个修改操作,但每个修改携带的数据包较小,可操作考虑批量操作.bulkWrite()改善性能. MongoCollection是线程安全的. db.c ...
- JS——scroll封装
DTD未声明:document.body.scrollTop DTD已声明:document.documentElement.scrollTop 火狐谷歌IE9:window.pageYOffset ...
- vsftpd:500OOPS:vsftpd:refusingtorunwithwritablerootinsidechroot()错误的解决方法
当我们限定了用户不能跳出其主目录之后,使用该用户登录FTP时往往会遇到这个错误: 500 OOPS: vsftpd: refusing to run with writable root inside ...