求最大三角形——poj2079

用旋转卡壳的思想,固定住一点,然后剩下两点通过单峰函数的性质进行移动

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

typedef double db;
const db eps=1e-8;
const db pi=acos(-1.0);
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 
struct point{
    db x,y;
    point(){}
    point(db x,db y):x(x),y(y){}
    point operator + (const point &k1) const{return point(k1.x+x,k1.y+y);}
    point operator - (const point &k1) const{return point(x-k1.x,y-k1.y);}
    point operator * (db k1) const{return point(x*k1,y*k1);}
    point operator / (db k1) const{return point(x/k1,y/k1);}
    bool operator < (const point k1) const{
        int a=cmp(x,k1.x);
        if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1;
    }
    db abs(){return sqrt(x*x+y*y);}
    db abs2(){return x*x+y*y;}
    db dis(point k1){return ((*this)-k1).abs();}
};
int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);}
db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;} 

int onS(point k1,point k2,point q){return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0;}
int checkconvex(vector<point>A){ //判断是否是凸包
    int n=A.size(); A.push_back(A[0]); A.push_back(A[1]);
    for (int i=0;i<n;i++) if (sign(cross(A[i+1]-A[i],A[i+2]-A[i]))==-1) return 0;
    return 1;
}
vector<point> ConvexHull(vector<point>A,int flag=1){ // 求凸包:flag=0 不严格 flag=1 严格 
    int n=A.size(); vector<point>ans(n*2); 
    sort(A.begin(),A.end()); int now=-1;
    for (int i=0;i<A.size();i++){
        while (now>0&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--;
        ans[++now]=A[i];
    } int pre=now;
    for (int i=n-2;i>=0;i--){
        while (now>pre&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--;
        ans[++now]=A[i];
    } ans.resize(now); return ans;
}

db RC(vector<point> q)
{
    int a=1,b=2;
    db ans=0;
    for(int i=0;i<q.size();i++){//固定点q[i]为第一个顶点 
        while(cross(q[a]-q[i],q[b]-q[i])<cross(q[a]-q[i],q[(b+1)%q.size()]-q[i]))
            b=(b+1)%q.size(); 
        ans=max(ans,cross(q[a]-q[i],q[b]-q[i]));
        while(cross(q[a]-q[i],q[b]-q[i])<cross(q[(a+1)%q.size()]-q[i],q[b]-q[i]))
            a=(a+1)%q.size();
        ans=max(ans,cross(q[a]-q[i],q[b]-q[i]));
    } 
    return ans;
}

int n;
vector<point> v;

int main(){
    while(cin>>n && n!=-1){
        v.clear();
        for(int i=1;i<=n;i++){
            point p;
            scanf("%lf%lf",&p.x,&p.y);
            v.push_back(p);
        }
        
        if(n<=2){puts("0.00");continue;}
        
        vector<point>res=ConvexHull(v,1);
        db ans=RC(res);
        printf("%.2f\n",ans/2);
    }
}

 

posted on 2020-02-26 00:44  zsben  阅读(206)  评论(0编辑  收藏  举报

导航