PKU 2079 Triangle

来源:互联网 发布:js 读取图片格式 编辑:程序博客网 时间:2024/06/05 04:45

 

Triangle

      

Description

Given n distinct points on a plane, your task is to findthe triangle that have the maximum area, whose vertices are from the givenpoints.

       平面上有n 个点,找出一个三角形有最大的面积,三角形的顶点来自给定的n个点。

Input

The input consists of several test cases. The first lineof each test case contains an integer n, indicating the number of points on theplane. Each of the following n lines contains two integer xi and yi, indicatingthe ith points. The last line of the input is an integer −1, indicating the endof input, which should not be processed. You may assume that 1 <= n <=50000 and −104 <= xi, yi <= 104 for all i= 1 . . . n.

       有多组测试数据,每组测试数据的第一行包括一个整数n,描述平面上有n个点。接下来的n行,每行包括两个整数xiyi,表示第i个点。最后一行是整数-1,表示输入结束。-1不需要处理。你可以假设: 1 <= n <= 50000 并且 −104 <= xi, yi <= 104 i = 1 . . . n.

Output

For each test case, print a line containing the maximumarea, which contains two digits after the decimal point. You may assume that thereis always an answer which is greater than zero.

       每组测试数据输出一行,包含一个数,最大的面积maxare并且保留有两个小数点,你可以假设结果都是大于0的。

Sample Input

3

3 4

2 6

2 7

5

2 6

3 9

2 0

8 0

6 5

-1

Sample Output

0.50

27.00

 

 

解题思路:

   具有最大面积的三角形的三个顶点必定在凸包上。

   证明:

1、     假设三个点都不在凸包上设为abc

ab为底边,c点到边的距离看做高hc,那么一定可以在凸包上找到一点ppab的距离hp大于hc。所以至少有一个点在凸包上。

2、     假设只有一个点在凸包上。设AbcA在凸包上)

假设Ab为底边,cAb的距离hc,那么一定可以在凸包上找到一点p,使pAb的距离hp大于hc,所以至少有两个点在凸包上。

3、     假设只有两个点在凸包上,设AbcAB在凸包上)

设底边ABcAB的距离hc,一定可以在凸包上找到一点p,使p点到AB的距离hp大于hc,故三个点必须都在凸包上。

第一步:

   nlognJarvis算法求凸包。

第二步:

1、     有以上证明故可以枚举凸包上的点,时间复杂度为,即O)(n为凸包上的点的个数)。

2、     可以枚举一个底边:然后用旋转卡壳的方法求距离该底边的最远距离的点。O)。

旋转卡壳过程描述:

   p[m]表示有m个顶点的凸包。以顶点p[i]为例:

1、     找到距离底边p[i],p[i+1]最远的点p[k];

2、     设距离底边p[i]p[j-1]最远的点为p[t];

对于距离底边p[i]p[j]最远的点为p[r]

s1=crossleft(p[i],p[j],p[t]);

s2=crossleft(p[i],p[j],p[t+1]);

if(s1>s2)

    p[r]=p[t];

else

    p[r]=p[t+1]; 

 

原创粉丝点击