POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)

Triangle
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 7625   Accepted: 2234

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

 
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std; struct Point
{
int x,y;
Point(int _x = , int _y = )
{
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 = ;
Point list[MAXN];
int Stack[MAXN],top;
bool _cmp(Point p1,Point p2)
{
int tmp = (p1-list[])^(p2-list[]);
if(tmp > )return true;
else if(tmp == && dist2(p1,list[]) <= dist2(p2,list[]))
return true;
else return false;
}
void Graham(int n)
{
Point p0;
int k = ;
p0 = list[];
for(int i = ;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[],list[k]);
sort(list+,list+n,_cmp);
if(n == )
{
top = ;
Stack[] = ;
return;
}
if(n == )
{
top = ;
Stack[] = ;
Stack[] = ;
return;
}
Stack[] = ;
Stack[] = ;
top = ;
for(int i = ;i < n;i++)
{
while(top > && ((list[Stack[top-]]-list[Stack[top-]])^(list[i]-list[Stack[top-]])) <= )
top--;
Stack[top++] = i;
}
}
//旋转卡壳,求两点间距离平方的最大值
int rotating_calipers(Point p[],int n)
{
int ans = ;
Point v;
int cur = ;
for(int i = ;i < n;i++)
{
int j = (i+)%n;
int k = (j+)%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+)%n]-p[k]) ) < )
k = (k+)%n;
j = (j+)%n;
}
}
return ans;
}
Point p[MAXN];
int main()
{
int n;
while(scanf("%d",&n) == )
{
if(n == -)break;
for(int i = ;i < n;i++)
list[i].input();
Graham(n);
for(int i = ;i < top;i++)
p[i] = list[Stack[i]];
int ans = rotating_calipers(p,top);
printf("%.2lf\n",ans/2.0);
}
return ;
}
 
 
上一篇:解决IOS safari在input focus弹出输入法时不支持position fixed的问题


下一篇:JavaScript Cookies,创建,获取cookies value