解决建筑桥梁的最长增加后续序列

时间:2013-10-19 18:24:44

标签: algorithm dynamic-programming lis

我有些困扰我的事情。我正在尝试解决将此信息作为指令的建筑桥梁问题。

  

建筑桥梁   考虑一个二维地图,水平河流穿过它的中心。南岸有n个城市,x坐标为a(1)... a(n),北岸有n个城市,x坐标为b(1)... b(n)。
  您希望尽可能多地将南北对的城市连接起来,以便没有两座桥梁交叉。连接城市时,您只能将北岸的城市i连接到南岸的城市i。“

我对Stack Overflow进行了研究,发现了一些对我有很大帮助的话题,如: Building bridges problem - how to apply longest increasing subsequence?
How to determine the longest increasing subsequence using dynamic programming?

但是当我提出LIS并尝试手动解决一个样本列表时。假设我有

Northern Bank >> 7 4 3 6 2 1 5
Southern Bank >> 5 3 2 4 6 1 7

排序南岸后

Northern Bank >> 1 3 4 6 7 2 5 
Southern Bank >> 1 2 3 4 5 6 7

哪个LIS长度假设为“5”但实际上在第一个列表中,只有3个桥能够创建(3,2,1)。那么我是否误解了LIS,或者是否有任何例外,这对构建桥梁问题无效?

1 个答案:

答案 0 :(得分:4)

我的理解是你已经使用增长最长的子序列正确地解决了问题:

enter image description here

你可以建造所有5个黑色桥梁(但不是2个红色桥梁)。

为什么你认为你只能建3座桥?