具有不同颜色的所有顶点的三角形的最大区域

时间:2016-10-17 04:48:36

标签: algorithm geometry area

笛卡尔平面中从(0,0)到(R,C)的点为r,g或b。使用这些点制作一个三角形 -

a) All three vertices are of different colors.
b) At least one side of triangle is parallel to either of the axes.
c) Area of the triangle is maximum possible.

输出最大可能区域。 约束:1<=R<=10001<=C<=1000

有人可以告诉我这个问题的方法吗?

1 个答案:

答案 0 :(得分:2)

三角形的区域为1/2 * base * height。因此,如果三角形的一边平行于
x轴,然后三角形的底边由同一行上的两种颜色(尽可能远地展开)形成,第三种颜色应该在距离底部最远的一行上。因此,您可以预处理数据以查找:

  1. 每种颜色的最顶部和最底部的行
  2. 每行上每种颜色的最左侧和最右侧列
  3. 然后对于每一行,你有十二种形成三角形的可能性,例如:

    left-most-red, right-most-blue, top-most green
    left-most-red, right-most-blue, bottom-most green
    left-most-red, right-most-green, top-most blue
    ...
    

    当然,对于三角形的相应过程,其中一条边与y轴平行。

    因此,问题可以在 O(R*C) 时解决,其中大部分时间都花在预处理上。

相关问题