您的位置:首页 > 其它

hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)

2014-08-16 16:38 429 查看
链接:http://poj.org/problem?id=2079
Triangle

Time Limit: 3000MSMemory Limit: 30000K
Total Submissions: 8173Accepted: 2423
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

--------------------------------------------------------------------------------------------------------------------------
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
乱起八糟的凸包,参考了n个凸包构建,但是构建方式都不同,有的凸包都还倒过来扫一遍,,,,,,不知道其中的差别
还有叉乘,叉乘没有深入理解
旋转卡壳还要重新看

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define eps 1e-8
#define MAXX 1000010

typedef struct point
{

double x;
double y;
}point;

bool dy(double x,double y){
return x>y+eps; }
bool xy(double x,double y){
return x<y-eps; }
bool dyd(double x,double y){
return x>y-eps; }
bool xyd(double x,double y){
return x<y+eps; }
bool dd(double x,double y){
return fabs(x-y)<eps; }

double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}

double dist(point a,point b)
{

return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}

point c[MAXX];
point stk[MAXX];
int top;

bool cmp(point a,point b)
{

double len=crossProduct(c[0],a,b);
if(dd(len,0.0))
return xy(dist(c[0],a),dist(c[0],b));
return xy(len,0.0);
}

double max(double x,double y)
{

return xy(x,y) ? y : x;
}

void Graham(int n)
{

int tmp=0;
for(int i=1; i<n; i++)
{

if(xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y))
tmp=i;
}
swap(c[0],c[tmp]);
sort(c+1,c+n,cmp);
stk[0]=c[0];
stk[1]=c[1];
top=1;
for(int i=2; i<n; i++)
{
while(top>=1 && xyd(crossProduct(stk[top],stk[top-1],c[i]),0.0))
top--;
stk[++top]=c[i];
}
}

double rotating(int n)
{
int j=1,k=2;
double ans=0.0;
stk
=stk[0];
for(int i=0; i<n; i++)
{
while(dy(fabs(crossProduct(stk[(k+1)%n],stk[i],stk[j])),fabs(crossProduct(stk[k],stk[i],stk[j]))))
k=(k+1)%n;
while(dy(fabs(crossProduct(stk[k],stk[i],stk[(j+1)%n])),fabs(crossProduct(stk[k],stk[i],stk[j]))))
j=(j+1)%n;
ans=max(ans,fabs(crossProduct(stk[k],stk[i],stk[j])));
}
return ans*0.5;
}

int main()
{

int i,j,n;
while(scanf("%d",&n)!=EOF&&n != -1)
{

for(i=0; i<n; i++)
scanf("%lf%lf",&c[i].x,&c[i].y);
Graham(n);//printf("%d**",top);
double ans=rotating(top+1);
printf("%.2lf\n",ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: