HDU 3934 Summer holiday(转卡壳计算平面点集最大三角形面积)

Summer holiday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1923    Accepted Submission(s): 694


Problem Description
Summer holiday was coming! Xiaomao went back to his hometown where he yearn day and night, his hometown has picturesque scenery. There is a big forest beside his village. There are n trees in the forest.
Now they want to across the forest with a rope (the rope won't cross). Try to find 3 trees in this tree on the rope which can make the area of the surrounded largest. Work out the area of it.
 

Input
The input will consist of several test cases. The first line contains a positive integer N(3<=N<=10^6), the number of trees, followed N lines, each gives the (xi, yi ) coordinates.
 

Output
Print the largest area, one number a line with two decimal places.
 

Sample Input
  
4 0 0 1 1 0 1 1 0
 

Sample Output
  
0.50
 

Source
题目大意:给定N个点,求这些点中任意三点围成的最大三角形的面积。
解题思路:先求出一个凸包包围这些点,然后用旋转卡壳求凸包内任意三角形面积比大小。

凸包模版
const int MAXN = 1000005;
Point list[MAXN];
int Stack[MAXN],top;
bool _cmp(Point p1,Point p2)
{
	int tmp = (p1-list[0])^(p2-list[0]);
    if(tmp > 0)return true;
    else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0]))
    return true;
    else return false;
}
void Graham(int n)
{
    Point p0;
    int k = 0;
    p0 = list[0];
    for(int i = 1;i < n;i++)
    if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x))
    {
        p0 = list[i];
        k = i;
    }
    swap(list[k],list[0]);
    sort(list+1,list+n,_cmp);
    if(n == 1)
    {
        top = 1;
        Stack[0] = 0;
        return;
    }
    if(n == 2)
    {
        top = 2;
        Stack[0] = 0; Stack[1] = 1;
        return;
    }
    Stack[0] = 0; Stack[1] = 1;
    top = 2;
    for(int i = 2;i < n;i++)
    {
        while(top > 1 &&((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0)
        top--;
        Stack[top++] = i;
    }
}
旋转卡壳计算平面点集最大三角形面积模版
int rotating_calipers(Point p[],int n)
{
    int ans = 0;
    Point v;
    for(int i = 0;i < n;i++)
    {
        int j = (i+1)%n;
        int k = (j+1)%n;
        while(j != i && k != i)
        {
            ans = max(ans,abs((p[j]-p[i])^(p[k]-p[i])));
            while( ((p[i]-p[j])^(p[(k+1)%n]-p[k])) < 0 )
            k = (k+1)%n;
            j = (j+1)%n;
        }
    }
    return ans;
}

以下为AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
struct Point
{
    int x,y;
    Point(int _x = 0,int _y = 0)
    {
        x = _x; y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x, y - b.y);
    }
    int operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    int operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    void input()
    {
        scanf("%d%d",&x,&y);
    }
};
//距离的平方
int dist2(Point a,Point b)
{
    return (a-b)*(a-b);
}
const int MAXN = 1000005;
Point list[MAXN];
int Stack[MAXN],top;
bool _cmp(Point p1,Point p2)
{
	int tmp = (p1-list[0])^(p2-list[0]);
    if(tmp > 0)return true;
    else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0]))
    return true;
    else return false;
}
void Graham(int n)
{
    Point p0;
    int k = 0;
    p0 = list[0];
    for(int i = 1;i < n;i++)
    if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x))
    {
        p0 = list[i];
        k = i;
    }
    swap(list[k],list[0]);
    sort(list+1,list+n,_cmp);
    if(n == 1)
    {
        top = 1;
        Stack[0] = 0;
        return;
    }
    if(n == 2)
    {
        top = 2;
        Stack[0] = 0; Stack[1] = 1;
        return;
    }
    Stack[0] = 0; Stack[1] = 1;
    top = 2;
    for(int i = 2;i < n;i++)
    {
        while(top > 1 &&((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0)
        top--;
        Stack[top++] = i;
    }
}
//旋转卡壳计算平面点集最大三角形面积
int rotating_calipers(Point p[],int n)
{
    int ans = 0;
    Point v;
    for(int i = 0;i < n;i++)
    {
        int j = (i+1)%n;
        int k = (j+1)%n;
        while(j != i && k != i)
        {
            ans = max(ans,abs((p[j]-p[i])^(p[k]-p[i])));
            while( ((p[i]-p[j])^(p[(k+1)%n]-p[k])) < 0 )
            k = (k+1)%n;
            j = (j+1)%n;
        }
    }
    return ans;
}
Point p[1000005];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i = 0;i < n;i++)list[i].input();
        Graham(n);
        for(int i = 0;i < top;i++)p[i] = list[Stack[i]];
        printf("%.2f\n",(double)rotating_calipers(p,top)/2);
    }
    return 0;
}
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=3934

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值