您的位置:首页 > 其它

poj 2079 Triangle(凸包+旋转卡壳)

2012-09-27 12:05 363 查看
Triangle

Time Limit: 3000MSMemory Limit: 30000K
Total Submissions: 7270Accepted: 2112
Description

Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.
Input

The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input
is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −104 <= xi, yi <= 104 for all i = 1 . . . n.
Output

For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.
Sample Input
3
3 4
2 6
2 7
5
2 6
3 9
2 0
8 0
6 5
-1

Sample Output
0.50
27.00

Source

Shanghai 2004 Preliminary
题目:http://poj.org/problem?id=2079

题意:给你n个点,求这n个点中能构成的最大面积的三角形

分析:首先,这个三角形必然在凸包上(我表示什么都不会= =),所以得先求出凸包,然后如果暴力枚举凸包的三个点恐怕是要超时的,接下来(我又不会了,又是看题解,权且当做是来拿模板的- -)设三个起点i=0,j=1,k=2,每次都先移动k,直到面积最大,然后移动j直到面积最大,然后移动i直到面积最大,因为这个过程中面积是不断增加的,所以移动完在取最大值吧,不用每移动一次都求,如果三个点都不变的话,强制移动k。。。

看看代码就明白了,很简单的代码,就是不会证明。。。貌似网上的一些代码有漏洞的样子,我不知道是正确性问题还是代码问题,网上一般都写当k!=0时运行,但是会有错,下面的数据

5

5 7

8 5

0 5

6 4

2 3

因为k=0并不代表已经转了一圈,我用一个数组标记k访问过的下标,来解决这个问题。。。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=55555;
typedef int diy;
struct point
{
    diy x,y;
    point(){}
    point(diy _x,diy _y):x(_x),y(_y){}
}g[mm],q[mm];
point Vector(point s,point t)
{
    return point(t.x-s.x,t.y-s.y);
}
diy CrossProduct(point P,point Q)
{
    return P.x*Q.y-P.y*Q.x;
}
diy MultiCross(point P,point Q,point R)
{
    return CrossProduct(Vector(Q,P),Vector(Q,R));
}
diy SqrDis(point P,point Q)
{
    return (P.x-Q.x)*(P.x-Q.x)+(P.y-Q.y)*(P.y-Q.y);
}
bool TurnRight(point P,point Q,point R)
{
    diy tmp=MultiCross(P,Q,R);
    if(tmp>0)return 1;
    if(tmp<0)return 0;
    return SqrDis(P,Q)<SqrDis(P,R);
}
bool cmp(point P,point Q)
{
    return TurnRight(g[0],Q,P);
}
void Graham(int n,int &m)
{
    int i,j;
    for(j=i=0;i<n;++i)
        if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i;
    swap(g[0],g[j]);
    sort(g+1,g+n,cmp);
    q[m=0]=g
=g[0];
    for(i=1;i<=n;++i)
    {
        while(m&&TurnRight(q[m-1],q[m],g[i]))--m;
        q[++m]=g[i];
    }
}
bool vis[mm];
diy RotateCaliper(int n)
{
    if(n<3)return 0;
    int i=0,j=1,k=2,ret=0,a,b,c;
    memset(vis,0,sizeof(vis));
    while(!vis[2])
    {
        a=i,b=j,c=k;
        while(MultiCross(q[k+1],q[j],q[i])>MultiCross(q[k],q[j],q[i]))vis[k=(k+1)%n]=1;
        while(MultiCross(q[k],q[j+1],q[i])>MultiCross(q[k],q[j],q[i]))j=(j+1)%n;
        while(MultiCross(q[k],q[j],q[i+1])>MultiCross(q[k],q[j],q[i]))i=(i+1)%n;
        ret=max(ret,MultiCross(q[k],q[j],q[i]));
        if(a==i&&b==j&&c==k)vis[k=(k+1)%n]=1;
    }
    return ret;
}
int main()
{
    int i,n,m;
    while(scanf("%d",&n)&&n>-1)
    {
        for(i=0;i<n;++i)
            scanf("%d%d",&g[i].x,&g[i].y);
        Graham(n,m);
        printf("%.2f\n",RotateCaliper(m)/2.0);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: