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 −10 4 <= xi, yi <= 10 4 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\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5434269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值