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 ≤ 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

  1. 4
  2. 1
  3. 5
  4. 3
  5. 2
  6. 1 3
  7. 3 2
  8. 3 4
  9. 9
  10. 1 2
  11. 1 3
  12. 1 4
  13. 2 3
  14. 2 1
  15. 2 4
  16. 3 1
  17. 3 2
  18. 3 4

Sample Output

  1. 4
  2. 2
  3. 2
  4. 0
  5. 0
  6. 0
  7. 0
  8. 2
  9. 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种可能的答案的最大值即可。
还有一些小细节,可以在代码中查看:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int n,a[],head[],to[],nextt[],tot,q,d[];
  9. int fa[][], minn[][], maxx[][], lmax[][], rmin[][];
  10.  
  11. void add(int x, int y)
  12. {
  13. to[tot] = y;
  14. nextt[tot] = head[x];
  15. head[x] = tot++;
  16. }
  17.  
  18. void dfs(int u, int from,int dist)
  19. {
  20. d[u] = dist;
  21. fa[u][] = from;
  22. for (int i = head[u]; i != -; i = nextt[i])
  23. {
  24. int v = to[i];
  25. if (from != v)
  26. dfs(v, u,dist + );
  27. }
  28. }
  29.  
  30. void init()
  31. {
  32. dfs(, , );
  33. minn[][] = maxx[][] = a[];
  34. lmax[][] = - << ;
  35. rmin[][] = << ;
  36. for (int i = ; i <= n; i++)
  37. {
  38. minn[i][] = min(a[i], a[fa[i][]]);
  39. maxx[i][] = max(a[i], a[fa[i][]]);
  40. lmax[i][] = max(a[fa[i][]] - a[i], );
  41. rmin[i][] = min(a[fa[i][]] - a[i], );
  42. }
  43. for (int j = ; ( << j) < n; j++)
  44. for (int i = ; i <= n; i++)
  45. {
  46. fa[i][j] = fa[fa[i][j - ]][j - ];
  47. maxx[i][j] = max(maxx[i][j - ], maxx[fa[i][j - ]][j - ]);
  48. minn[i][j] = min(minn[i][j - ], minn[fa[i][j - ]][j - ]);
  49. lmax[i][j] = max(max(lmax[i][j - ], lmax[fa[i][j - ]][j - ]), maxx[fa[i][j-]][j - ] - minn[i][j - ]);
  50. rmin[i][j] = min(min(rmin[i][j - ], rmin[fa[i][j - ]][j - ]), minn[fa[i][j - ]][j - ] - maxx[i][j - ]);
  51. }
  52. }
  53.  
  54. int LCA(int x, int y)
  55. {
  56. int maxxx = , miny = , minx = a[x], maxy = a[y];
  57. for (int i = ; i >= && d[x] != d[y]; i--)
  58. {
  59. if (abs(d[x] - d[y]) >= << i)
  60. {
  61. if (d[y] < d[x])
  62. {
  63. maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx);
  64. minx = min(minx, minn[x][i]);
  65. x = fa[x][i];
  66. }
  67. else
  68. {
  69. miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy);
  70. maxy = max(maxy, maxx[y][i]);
  71. y = fa[y][i];
  72. }
  73. }
  74. }
  75. if (x == y)
  76. return max(max(maxxx, -miny), maxy - minx);
  77. for (int i = ; i >= ;i--)
  78. if (fa[x][i] != fa[y][i] && fa[x][i] && fa[y][i])
  79. {
  80. maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx);
  81. minx = min(minx, minn[x][i]);
  82. x = fa[x][i];
  83.  
  84. miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy);
  85. maxy = max(maxy, maxx[y][i]);
  86. y = fa[y][i];
  87. }
  88. maxxx = max(max(maxxx, lmax[x][]), maxx[x][] - minx);
  89. minx = min(minx, minn[x][]);
  90. miny = min(min(miny, rmin[y][]), minn[y][] - maxy);
  91. maxy = max(maxy, maxx[y][]);
  92. return max(max(maxxx, -miny), maxy - minx);
  93.  
  94. }
  95.  
  96. int main()
  97. {
  98. memset(head, -, sizeof(head));
  99. scanf("%d", &n);
  100. for (int i = ; i <= n; i++)
  101. scanf("%d", &a[i]);
  102. for (int i = ; i <= n - ; i++)
  103. {
  104. int u, v;
  105. scanf("%d%d", &u, &v);
  106. add(u, v);
  107. add(v, u);
  108. }
  109. init();
  110. scanf("%d", &q);
  111. while (q--)
  112. {
  113. int x, y;
  114. scanf("%d%d", &x, &y);
  115. printf("%d\n", LCA(x, y));
  116. }
  117.  
  118. return ;
  119. }

poj3728The merchant的更多相关文章

  1. poj3728The merchant 【倍增】【LCA】

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  2. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  3. poj3728The merchant树剖+线段树

    如果直接在一条直线上,那么就建线段树 考虑每一个区间维护最小值和最大值和答案,就符合了合并的条件,一个log轻松做 那么在树上只要套一个树剖就搞定了,多一个log也不是问题 注意考虑在树上的话每一条链 ...

  4. [最近公共祖先] POJ 3728 The merchant

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 Desc ...

  5. POJ 3278 The merchant

    传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there i ...

  6. poj 3728 The merchant(LCA)

    Description There are N cities in a country, and there is one and only one simple path between each ...

  7. ThoughtWorks Merchant's Guide To The Galaxy

    ThoughtWorks笔试题之Merchant's Guide To The Galaxy解析 一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天H ...

  8. [POJ 3728]The merchant

    Description There are N cities in a country, and there is one and only one simple path between each ...

  9. opencart3图片Google Merchant Center验证通过不了的解决方法

    最近在做一个opencart项目,有对接Google Merchant Center,但是一直提示产品图片验证无法通过,ytkah看了一下图片路径,/image/cache/catalog/demo/ ...

随机推荐

  1. PWD简介与妙用(一个免费、随时可用的Docker实验室)

    转载自 https://baiyue.one/archives/472.html 本文介绍下 PWD 的历史,并依据本站最近学习心得,经过多次尝试,终于打通了 Docker 与常规宝塔面板搭建,因此, ...

  2. 二、antd pro 删除eslint检测

    删除package.json 里 " pre-commit": "npm run lint-staged" 这个对象就可以.

  3. 2018.3.11 Java DEBUG 调试的方法

    F5:Step Into:跳进当前执行的方法中 F6:Step Over:跳过该行继续执行 F7:Step Return:从方法中跳出继续执行 F8:Resume:全部运行通过,从调试状态恢复 Ctr ...

  4. 使用lua做序列化和反序列化

    -- lua对象序列化 function serialize(obj) local lua = "" local t = type(obj) if t == "numbe ...

  5. Unity3d 中键值监听方法

    unity3d的api中没有负责监听键值的方法,不过unity的input类是通过c#类获取各类监听事件,所以我们可以通过c#类监听,方法如下: void OnGUI() { Event e = Ev ...

  6. 【kmp】bzoj3620: 似乎在梦中见过的样子

    考察kmp理解题 Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中 ...

  7. JWT的使用流程

    JWT的实现原理 一篇文章告诉你JWT的实现原理 发布于 3 个月前 作者 axetroy 3097 次浏览 来自 分享 在使用 JWT 的时候,有没有想过,为什么我们需要 JWT?以及它的工作原理是 ...

  8. linux常用命令(配置查看,定时任务)

    1.查看所有待挂载设备信息 fdisk -l # fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders, total sector ...

  9. Python3 简单封装 sqlite3 - SimpleToolSql

    #coding: utf-8 #Author:boxker #Mail:icjb@foxmail.com import sqlite3 import os class simpleToolSql(): ...

  10. angular5自适应窗口大小

    import {AfterViewInit, Directive, ElementRef, HostBinding, HostListener, Inject, Input, Renderer2} f ...