POJ2079:Triangle——题解

http://poj.org/problem?id=2079

题目大意:求最大面积的三角形。

——————————————————

可以知道,最大面积的三角形的顶点一定是最大凸包的顶点。

接下来就是O(n*n)的常数优化题了(利用单峰性)。

(但其实不是n*n的,因为我们求的是纯凸包,所以n会小一些)

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double dl;
const dl eps=1e-9;
const int N=50001;
struct point{
    dl x;
    dl y;
}p[N],q[N];
int n,per[N],l;
inline point getmag(point a,point b){
    point s;
    s.x=b.x-a.x;s.y=b.y-a.y;
    return s;
}
inline dl multiX(point a,point b){
    return a.x*b.y-b.x*a.y;
}
inline dl dis(point a,point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline bool cmp(int u,int v){
    dl det=multiX(getmag(p[1],p[u]),getmag(p[1],p[v]));
    if(fabs(det)>eps)return det>eps;
    return dis(p[1],p[u])-dis(p[1],p[v])<-eps;
}
void graham(){
    int id=1;
    for(int i=2;i<=n;i++){
    if(p[i].x-p[id].x<-eps||(fabs(p[i].x-p[id].x)<eps&&p[i].y-p[id].y<-eps))id=i;
    }
    if(id!=1)swap(p[1],p[id]);
    for(int i=1;i<=n;i++)per[i]=i;
    sort(per+2,per+n+1,cmp);
    l=0;
    q[++l]=p[1];
    for(int i=2;i<=n;i++){
    int j=per[i];
    while(l>=2&&multiX(getmag(q[l-1],p[j]),getmag(q[l-1],q[l]))>-eps){
        l--;
    }
    q[++l]=p[j];
    }
    return;
}
inline dl area(){
    if(l<=2)return 0;
    dl ans=0;
    for(int i=1;i<=l;i++){
    int j=i%l+1;
    int k=j%l+1;
    while(233){
        dl s1=multiX(getmag(q[i],q[j]),getmag(q[i],q[k]));
        dl s2=multiX(getmag(q[i],q[j]),getmag(q[i],q[k%l+1]));
        if(fabs(s1)-fabs(s2)>-eps){
        break;
        }
        k=k%l+1;
    }
    while(i!=j&&j!=k&&i!=k){
        dl s=multiX(getmag(q[i],q[j]),getmag(q[i],q[k]));
        ans=max(ans,fabs(s)/2.0);
        while(233){
        dl s1=multiX(getmag(q[i],q[j]),getmag(q[i],q[k]));
        dl s2=multiX(getmag(q[i],q[j]),getmag(q[i],q[k%l+1]));
        if(fabs(s1)-fabs(s2)>-eps){
            break;
        }
        k=k%l+1;
        }
        j=j%l+1;
    }
    }
    return ans;
}
int main(){
    while(scanf("%d",&n)!=EOF&&n!=-1){
    for(int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
    graham();
    printf("%.2f\n",area());
    }
    return 0;
}

 

posted @ 2017-12-26 18:57  luyouqi233  阅读(261)  评论(0编辑  收藏  举报