zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        }
    
        return 0;
    }
  • 相关阅读:
    Python 操作 MySQL数据库提示pymysql.err.InternalError: (1054, "Unknown column 'XXXXXXXXX' in 'where clause'")解决方法
    MySQL连接池不能查到刚写入的数据——连接池配置问题
    python 将字典转为bytes类型字典
    关于状态机的问题思考——什么时候达到新的状态?什么时候清除老状态?新状态与老状态之间的关系
    mysql 8.0.19 安装 及 端口修改
    sprintf printf 输出数据固定格式——数字前补零
    思维大爆炸
    IO点作为状态判断——一定要做软件“消抖”
    React-umi-request动态刷新Token功能实现及node.js 代码逻辑
    js测试题
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8126207.html
Copyright © 2011-2022 走看看