pku2079wa

来源:互联网 发布:费洛伊德算法 编辑:程序博客网 时间:2024/05/01 05:11

题意:在一些点中找出面积最大的三角形

思路:求出凸包 在凸包中遍历所有三角形找不最大的

#include<iostream>#include<stdio.h>#include<cmath>#include<algorithm>  #define MAX 50010using namespace std;typedef struct point{    double x,y;};point p[MAX],pnt[MAX],res[MAX];bool multi(point p1,point p2,point p0) //[p0,p1]和[p0,p2]的叉积 保持p1,p2,p0的顺序    {   return (p1.x-p0.x)*(p2.y-p0.y)>=(p2.x-p0.x)*(p1.y-p0.y);}   bool operator < (const point &l, const point &r){   return l.y < r.y || (l.y == r.y && l.x < r.x);  } int graham(point pnt[], int n, point res[])   {   int i, len, k = 0, top = 1;        sort(pnt, pnt + n);        if (n == 0) return 0; res[0] = pnt[0];        if (n == 1) return 1; res[1] = pnt[1];        if (n == 2) return 2; res[2] = pnt[2];        for (i = 2; i < n; i++)        {  while (top && multi(pnt[i], res[top], res[top-1])) top--;           res[++top] = pnt[i]; //cout<<res[top].x<<"dddd"<<endl;       }        len = top; res[++top] = pnt[n - 2];        for (i = n - 3; i >= 0; i--)       {    while (top!=len && multi(pnt[i], res[top], res[top-1])) top--;             res[++top] = pnt[i];        }        return top;       // 返回凸包中点的个数    }    double Area(double a,double b,double c){    double p=(a+b+c)*1.0/2;    return sqrt(p*(p-a)*(p-b)*(p-c));}int main(){    int n,top,i,j,k;        while(cin>>n&&n!=-1)    {    double max=0;         for(i=0;i<n;i++)            cin>>pnt[i].x>>pnt[i].y;         top=graham(pnt, n, res);//cout<<top<<endl;         for(i=0;i<top;i++)         {              for(j=i+1;j<top;j++)            {   double a=sqrt((res[i].x-res[j].x)*(res[i].x-res[j].x)+(res[i].y-res[j].y)*(res[i].y-res[j].y));                for(k=j+1;k<top;k++)                {                   double b=sqrt((res[i].x-res[k].x)*(res[i].x-res[k].x)+(res[i].y-res[k].y)*(res[i].y-res[k].y));                     double c=sqrt((res[j].x-res[k].x)*(res[j].x-res[k].x)+(res[j].y-res[k].y)*(res[j].y-res[k].y));                    double s=Area(a,b,c);                     if(s>max) max=s;                       }            }         }        printf("%.2lf/n",max);    }   // system("pause");    return 0;}http://acm.pku.edu.cn/JudgeOnline/problem?id=2079