poj 2079(旋转卡壳求解凸包内最大三角形面积)

Triangle
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 9060   Accepted: 2698

Description

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

Input

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

Output

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

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

Source

旋转卡壳算法可以参见我的上一篇博客以及里面的链接:http://www.cnblogs.com/liyinggang/p/5431908.html

题意:求解平面中的点中任意取三个能够形成最大的三角形面积。

题解:先用凸包把所有可能的点选出来,最大三角形必定是由凸包上的三点形成。

我们枚举底边,于是我们可以的到以下两种情况:

1.此三角形的底边在凸包上,求得次边对应的最远的点(不是对踵点),由于凸包是个单峰函数,所以只要找到第一个这个点比上一个点

大就找到了。记录下此时的面积(对应黄色线条).

2.如果三角形底边不再凸包上,我们利用同样的方法找到离此底边最远的点(对应红色线条)

1,2相比,取大值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 50005;
struct Point{
    int x,y;
}p[N],Stack[N];
int n;

int mult(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dis(Point a,Point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(Point a,Point b){
    if(mult(a,b,p[0])>0) return 1;
    if(mult(a,b,p[0])==0&&dis(b,p[0])>dis(a,p[0])) return 1;
    return 0;
}
int Graham(){
    sort(p+1,p+n,cmp);
    int top = 2;
    Stack[0]=p[0];
    Stack[1]=p[1];
    Stack[2]=p[2];
    for(int i=3;i<n;i++){
        while(top>=1&&mult(p[i],Stack[top],Stack[top-1])>=0){
            top--;
        }
        Stack[++top]=p[i];
    }
    return top;
}
double rotating_calipers(int top){
   int p=1,q=2; ///初始化
   double ans = 0;
   Stack[++top]=Stack[0];
   for(int i = 0;i<top;i++){
        while(mult(Stack[i],Stack[p],Stack[q+1])>mult(Stack[i],Stack[p],Stack[q])){
            q= (q+1)%top; ///定点i,p,q,先I,p固定,让q旋转找到最大的面积三角形,还是利用了凸包的单峰函数
        }
        ans = max(ans,mult(Stack[i],Stack[p],Stack[q])/2.0);
        while(mult(Stack[i],Stack[p+1],Stack[q])>mult(Stack[i],Stack[p],Stack[q])){
            p=(p+1)%top; ///i,q固定,p旋转,找到最大的三角形面积,比较记录.
        }
        ans = max(ans,mult(Stack[i],Stack[p],Stack[q])/2.0);
   }
   return ans;
}
int main()
{
    while(scanf("%d",&n)!=EOF,n!=-1){
        for(int i=0;i<n;i++){
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        int k = 0;
        for(int i=1;i<n;i++){
            if(p[k].y>p[i].y||(p[k].y==p[i].y)&&(p[k].x>p[i].x)){
                k=i;
            }
        }
        swap(p[0],p[k]);
        int top = Graham();
        double ans =rotating_calipers(top);
        printf("%.2lf
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5434269.html