好久没写几何题了。。。
以前都是直接套模板 现在打算重新练练 理解其中的意思 不能一通乱套。。。
题意:给你一个二维点集 问你它们能够成的最大三角形面积
思路:这个题n = 50000 O(n^3)必T 不难证明最大三角形的点一定在凸包上 所以我们可以先求出凸包 但是极限情况下还是O(n^3)... 这时就用到给力的旋转卡(qia)壳了 首先我们枚举i 然后固定i的下一个点j 求第一个满足Cross(i, j, k)大于Cross(i,j,k+1)的点k 令kk = k 然后旋转j和kk 当Cross(i, j, kk)大于Cross(i,j,kk+1)时 j++ 而kk只需继续往下走即可 具体实现看代码吧...
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define REV( i, a, b ) for( int i = a; i >= b; i-- )
const double eps = 1e-8;
struct Point{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector;
int dcmp(double x){
if(fabs(x) < eps) return 0;
return x > eps ? 1 : -1;
}
Vector operator - (const Vector &A, const Vector &B){ return Vector(A.x - B.x, A.y - B.y); }
bool operator == (const Point &A, const Point &B){ return !dcmp(A.x - B.x) && !dcmp(A.y - B.y); }
bool operator < (const Point &a, const Point &b) { return a.x + eps < b.x || (!dcmp(a.x - b.x) && a.y + eps < b.y); }
double Cross(const Vector &A, const Vector &B){ return A.x * B.y - A.y * B.x; }
vector<Point> ConvexHull(vector<Point>& p) {
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
int n = p.size();
int m = 0;
vector<Point> ch(n+1);
REP(i, 0, n){
while(m > 1 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
REV(i, n - 2, 0){
while(m > k && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
ch.resize(m);
return ch;
}
double RC(vector<Point> p){
int j, k, n = p.size();
double max_area = 0;
REP(i, 0, n){
j = (i + 1) % n, k = (j + 1) % n;
while( dcmp( Cross(p[i] - p[j], p[i] - p[k]) - Cross(p[i] - p[j], p[i] - p[(k+1)%n]) ) < 0 )
k = (k + 1) % n;
max_area = max( max_area, fabs( Cross(p[i] - p[j], p[i] - p[k]) ) );
int kk = (k + 1) % n;
while(j != kk && kk != i){
while(dcmp( Cross(p[i] - p[j], p[i] - p[kk]) - Cross(p[i] - p[j], p[i] - p[(kk+1)%n]) )<0)
kk = (kk + 1) % n;
max_area = max( max_area, fabs( Cross(p[i] - p[j], p[i] - p[kk]) ) );
j = (j + 1) % n;
}
}
return max_area / 2;
}
Point Read_Point(){
Point p;
scanf("%lf%lf", &p.x, &p.y);
return p;
}
void solve(int n){
vector<Point> p;
REP(i, 0, n) p.push_back( Read_Point() );
printf("%.2lf\n", RC( ConvexHull(p) ) );
}
int main()
{
//freopen("in.txt", "r", stdin);
int n;
while(scanf("%d", &n) && ~n) solve(n);
return 0;
}