poj 3728 The merchant// lca(倍增实现) + dp || tarjan+并查集路径上dp

44 篇文章 0 订阅
14 篇文章 0 订阅

poj 3728 The merchant// lca(倍增实现) + dp

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6437 Accepted: 2251

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

题意:给出一个树,结点有一个w[i],给出q个询问,每个询问给出u和v,在u->v的路径中选出两个点a,b(a,b可以一样,a比b先出现),求出最大的w[b]-w[a]。

对于一个询问u,v,ans=max(u到lca(u,v)之间的最大值,lca(u,v)到v之间的最大值,根节点到v之间的最大值-根节点到u之间的最小值)。

利用倍增思想,处理出fa[v][k](v号结点上升2^k所到的结点),mx[v][k],mi[v][k](v号结点上升2^k所到结点x,x,v之间的最大值及最小值),up[v][k](v号结点上升2^k所到结点x,v和x之间能赚到钱的最大值)down[v][k](从距离v  2^k步所在的结点x下降到v,x和v之间能赚到钱的最大值)。

具体dp转移的过程看注释。这题还是挺有质量的。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
const int max_n=5e4+5,max_logn=24;
struct no{int to,ne;};
no e[max_n*2];int head[max_n],edgenum;
void initvector(){edgenum=0;memset(head,-1,sizeof(head));}
void addedge(int from,int to){//头插法
    no E={to,head[from]};
    e[edgenum]=E;head[from]=edgenum++;
}

int fa[max_n][max_logn],de[max_n],root=1;
int n,w[max_n];
int mx[max_n][max_logn],mi[max_n][max_logn];
int up[max_n][max_logn],down[max_n][max_logn];

void dfs(int v,int p,int d){//所有都是向上走2^k步 所代表的含义
    fa[v][0]=p;de[v]=d;
    mx[v][0]=max(w[v],w[p]);mi[v][0]=min(w[v],w[p]);//只走2^0次步 预处理
    up[v][0]=max(w[p]-w[v],0);
    down[v][0]=max(w[v]-w[p],0);
    for(int i=head[v];i!=-1;i=e[i].ne){
        if(e[i].to!=p)dfs(e[i].to,v,d+1);
    }
}
void init(int V){//预处理fa,dp的数组
    dfs(root,-1,0);
    for(int k=0;k+1<max_logn;k++){
        for(int v=1;v<=V;v++){
            int tfa=fa[v][k];
            if(tfa<0) fa[v][k+1]=-1;
            else {//可达
                fa[v][k+1]=fa[tfa][k];
                mx[v][k+1]=max(mx[v][k],mx[tfa][k]);
                mi[v][k+1]=min(mi[v][k],mi[tfa][k]);
                up[v][k+1]=max(up[v][k],up[tfa][k]);
                up[v][k+1]=max(up[v][k+1],mx[tfa][k]-mi[v][k]);//跨两个小区间
                down[v][k+1]=max(down[v][k],down[tfa][k]);
                down[v][k+1]=max(down[v][k+1],mx[v][k]-mi[tfa][k]);//和up相反
            }
        }
    }
}
int find_lca(int u,int v){
    if(de[u]>de[v]) swap(u,v);
    for(int k=0;k<max_logn;k++){
        if((de[v]-de[u])>>k&1) v=fa[v][k];
    }if(u==v) return u;
    for(int k=max_logn-1;k>=0;k--){
        if(fa[u][k]!=fa[v][k]){u=fa[u][k],v=fa[v][k];}
    }
    return fa[u][0];
}
int find_up(int u,int lca,int &remin){
    remin=INT_MAX/2;
    int maxup=0,pre_min=INT_MAX/2;//维护前缀最小值
    for(int k=max_logn-1;k>=0;k--){
        if((de[u]-de[lca])>>k&1){
            remin=min(remin,mi[u][k]);
            maxup=max(maxup,up[u][k]);
            maxup=max(maxup,mx[u][k]-pre_min);//跨小区间跟新
            pre_min=min(pre_min,mi[u][k]);
            u=fa[u][k];
        }
    }
    return maxup;
}
int find_down(int v,int lca,int &remax){
    remax=INT_MIN/2;
    int maxdown=0,pre_max=INT_MIN/2;
    for(int k=max_logn-1;k>=0;k--){
        if((de[v]-de[lca])>>k&1){
            remax=max(remax,mx[v][k]);
            maxdown=max(maxdown,down[v][k]);
            maxdown=max(maxdown,pre_max-mi[v][k]);
            pre_max=max(pre_max,mx[v][k]);
            v=fa[v][k];
        }
    }
    return maxdown;
}
int find_ans(int u,int v){
    int lca=find_lca(u,v);int re=0;
    int up_min_val=INT_MAX/2,down_max_val=INT_MIN/2;
    re=max(re,find_up(u,lca,up_min_val));//u-lca
    re=max(re,find_down(v,lca,down_max_val));//lca-v
    re=max(re,(down_max_val-up_min_val));//包含根节点
    return re;
}
int main(){
    initvector();
    scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&w[i]);
    for(int i=1;i<n;i++){
        int u,v;scanf("%d%d",&u,&v);
        addedge(u,v),addedge(v,u);
    }
    init(n);
    int q;scanf("%d",&q);while(q--){
        int xx,yy;scanf("%d%d",&xx,&yy);
        printf("%d\n",find_ans(xx,yy));
    }
    return 0;
}

4.24更:

终于肝出来了并查集路径下dp+离线tarjanlca的解法

照样维护up,down,mi,mx,不过都是当前状态下的。

#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int max_n= 5e4+5;
struct qno{int to,id;};
vector<qno>q[max_n];//询问
int ans[max_n];
vector<int>g[max_n];//树
int w[max_n],uu[max_n],vv[max_n];int n,Q;//uu:询问的起点,vv:询问的终点
int fa[max_n],vis[max_n];
int up[max_n],down[max_n],mx[max_n],mi[max_n];//都是在当前状态下,到父节点的关系
struct lcano{int x,id;};//lca下的询问
vector<lcano>qq[max_n];
int found(int x){//并查集路径下dp
    if(fa[x]==x)return x;
    int t=fa[x];
    fa[x]=found(fa[x]);
    up[x]=max(up[t],up[x]);
    up[x]=max(up[x],mx[t]-mi[x]);
    down[x]=max(down[t],down[x]);
    down[x]=max(down[x],mx[x]-mi[t]);
    mx[x]=max(mx[t],mx[x]);
    mi[x]=min(mi[t],mi[x]);
    return fa[x];
}

void tarjan(int u){
    vis[u]=true;
    for(int i=0;i<q[u].size();i++){
        int qid=q[u][i].id,to=q[u][i].to; //lca为found(to)
        if(vis[to]){
            int lca=found(to);
            qq[lca].push_back(lcano{to,qid});//先存起来,只有lca以下的节点都访问过了才可以计算
        }

    }
    for(int i=0;i<g[u].size();i++){
        int to=g[u][i];
        if(!vis[to]){
            tarjan(to);
            fa[to]=u; 
        }
    }
    for(int i=0;i<qq[u].size();i++){//u子树都访问过了,那么u做为询问的lca,可以执行lca是u的询问,回溯的遍历顺序保证正确可行性
        int id=qq[u][i].id;
        found(uu[id]),found(vv[id]);//左边更新,右边跟新
        ans[id]=max(up[uu[id]],down[vv[id]]);
        ans[id]=max(ans[id],mx[vv[id]]-mi[uu[id]]);
    }

}
int main(){
    scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&w[i]);
     for(int u=1;u<=n;u++){
        fa[u]=u;up[u]=0;down[u]=0;mx[u]=w[u];mi[u]=w[u];
    }
    for(int i=1;i<n;i++){
        int scu,scv;scanf("%d%d",&scu,&scv);
        g[scu].push_back(scv);g[scv].push_back(scu);
    }
    scanf("%d",&Q);for(int i=1;i<=Q;i++){
        scanf("%d%d",&uu[i],&vv[i]);
        q[uu[i]].push_back(qno{vv[i],i});
        q[vv[i]].push_back(qno{uu[i],i});
    }
    tarjan(1);
    for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
    return 0;
}
/*6
5 3 1 4 2 7
1 2
1 3
2 4
2 5
3 6
2
2 4
4 2*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值