poj2079 Triangle

Triangle
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 9992   Accepted: 3001

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

大致题意:给n个点,求面积最大的三角形.
分析:面积最大的三角形的点肯定都在凸包上.最开始的想法是枚举其中两个点,让第三个点单调地去扫,会T掉,其实不光第三个点有单调性,第二个点也有单调性,可以先让第三个点走,当到达最大值时更新答案并让第二个点走,循环直到得到答案.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const double eps = 1e-8;

int n,tot;
double ans;
struct node
{
    double x,y;
    node() {}
    node(double _x,double _y) :x(_x),y(_y) {}
} e[50010],p[50010];

double dist(node a,node b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double det(node a,node b)
{
    return a.x * b.y - a.y * b.x;
}

double dot(node a,node b)
{
    return a.x * b.x + a.y * b.y;
}

node operator + (node a,node b)
{
    return node(a.x + b.x,a.y + b.y);
}

node operator - (node a,node b)
{
    return node(a.x - b.x,a.y - b.y);
}

node operator * (node a,double x)
{
    return node(a.x * x,a.y * x);
}

bool cmp(node a,node b)
{
    double temp = det(a - e[1],b - e[1]);
    if (temp != 0)
        return temp > 0;
    return dist(a,e[1]) < dist(b,e[1]);
}

void solve()
{
    int id = 1;
    for (int i = 1; i <= n; i++)
        if (e[i].x < e[id].x ||(e[i].x == e[id].x && e[i].y < e[id].y))
            id = i;
    if (id != 1)
        swap(e[1],e[id]);
    sort(e + 2,e + 1 + n,cmp);
    p[++tot] = e[1];
    for (int i = 2; i <= n; i++)
    {
        while (tot >= 2 && det(e[i] - p[tot - 1],p[tot] - p[tot - 1]) >= 0)
            tot--;
        p[++tot] = e[i];
    }
}

void solve2()
{
    p[tot + 1] = p[1];
    int j,k;
    for (int i = 1; i <= tot; i++)
    {
        j = i % tot + 1;
        k = j % tot + 1;
        while (i != j && j != k && i != k)
        {
            while (det(p[k + 1] - p[j],p[i] - p[j]) >= det(p[k] - p[j],p[i] - p[j]))
                k = k % tot + 1;
            ans = max(ans,fabs(det(p[k] - p[j], p[i] - p[j])) / 2);
            j = j % tot + 1;
        }
    }
}

int main()
{
    while (scanf("%d",&n) != EOF && n != -1)
    {
        for (int i = 1; i <= n; i++)
            scanf("%lf%lf",&e[i].x,&e[i].y);
        tot = 0;
        ans = 0.0;
        solve();
        solve2();
        printf("%.2lf\n",ans);
    }

    return 0;
}

 

posted @ 2017-12-27 15:55  zbtrs  阅读(168)  评论(0编辑  收藏  举报