hunnu10701(在n个点中选三个点,使得构成的面积最大)

解题思路:
1、先求n个点的凸包,因为这三个点肯定在凸包上;
2、用旋转卡壳法求最大面积
代码如下:

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>

#define N 50005
#define eps 1e-9
#define P system("pause")
using namespace std;
struct node
{
       double x,y;     
}p[N],ch[N];
node operator - (node a,node b)
{
     a.x -= b.x;
     a.y -= b.y;
     return a;
}
double cross(node a, node b)
{
       return a.x*b.y - a.y*b.x;        
}
bool cmp(node a,node b)
{
     if(a.x != b.x)
        return a.x < b.x;
     return a.y < b.y;     
}
double area(node a,node b,node c)
{
       return fabs(cross(a-b,a-c)/2);
}
int convexhull(int n) //求凸包模板
{
    sort(p,p+n,cmp);  
    int i,m = 0;
    for(i=0; i<n; i++)
    {
         while(m>1 && cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) 
                   m--;
         ch[m++] = p[i];              
    }  
    int k = m;
    for(i = n-2; i >=0; i--)
    {
          while(m > k && cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)
                  m--;
          ch[m++] = p[i];      
    }
    if(n > 1) m--;
    return m;
}

int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);cc
    int n;
    while(scanf("%d",&n) && n)
    {
          int i;
          for(i=0;i<n;i++)
                          scanf("%lf%lf",&p[i].x,&p[i].y);
          int top = convexhull(n);
       //   for(i= 0; i < top; i++)
         //      cout<<ch[i].x<<" "<<ch[i].y<<endl;
          double ans = 0;
          int j,k;
          for(i = 0; i < top; i++)  //求每个点i对应的最大三角形 
          {
                j = (i + 1)%top;
                k = (j + 1)%top;
                while(k != i && area(ch[i],ch[j],ch[k]) < area(ch[i], ch[j], ch[(k+1)%top]))
                        k = (k+1)%top;
                if(k == i) continue;
                int kk = (k+1)%top;
                while(j != kk && k != i)
                {
                        ans = max(ans,area(ch[i],ch[j],ch[k]));
                        while(k != i && area(ch[i],ch[j],ch[k]) < area(ch[i],ch[j],ch[(k+1)%top]))
                                k = (k+1)%top;
                        j = (j+1)%top;        
                }      
          }
          printf("%0.2lf\n",ans);
    }
  //  P;                               
    return 0;    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值