HDU 2202最大三角形

来源:互联网 发布:阿里云 cdn combo请求 编辑:程序博客网 时间:2024/05/21 16:30
#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;const int maxn=50005;struct node{    int x,y;}e[maxn],res[maxn];int cmp(node a,node b){    if(a.x==b.x)return a.y<b.y;    return a.x<b.x;}int cross(node a,node b,node c){    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);}int convex(int n){    int top = -1;    res[++top] = e[0];    res[++top] = e[1];    for(int i=2;i<n;i++)    {        while(top && cross(res[top],e[i],res[top-1])<=0)            top--;        res[++top] = e[i];    }    int midtop = top;    for(int i=n-2;i>=0;i--)    {        while(top>midtop && cross(res[top],e[i],res[top-1])<=0)        {            top--;        }        res[++top] =e[i];    }    return top;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int i,j,k,x,y,m;        for(i=0;i<n;i++)            scanf("%d%d",&e[i].x,&e[i].y);        sort(e,e+n,cmp);        m=convex(n);        int ans=0;        for(i=0;i<m;i++)            for(j=i+1;j<m;j++)                for(k=j+1;k<m;k++)                ans=max(ans,cross(res[j],res[k],res[i]));        printf("%.2lf\n",0.5*ans);    }    return 0;}/*    水平序的Andrew算法,求凸包。时间复杂度O(n)    最大三角形的三个顶点必定在凸包上,枚举即可。    向量积:a×b=x1*y2-x2*y1;当a×b>0时b在a的逆时针方向,a×b<0时b在顺时针方向,a×b=0时共线    |a×b|=|a|*|b|*sin<a,b>,可以求三角形面积。*/

0 0