zoukankan      html  css  js  c++  java
  • Triangle POJ

    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 −10 4 <= xi, yi <= 10 4 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

    SOLUTION:
    板子:

    CODE:
    #include <iostream>
    #include <cstdio>
    #include"cmath"
    #include"iomanip"
    #include <algorithm>
    using namespace std;
    const int N=5e4+10;
    struct point{
        int x,y;
    }p[N];
    int dis(point a,point b){
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    int cross(point p0,point p1,point p2){
        return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    }
    int cmp1(point p1,point p2){
        return p1.x<p2.x||(p1.x==p2.x&&p1.y<p2.y);
    }
    int cmp2(point p1,point p2){
    
    
        int c=cross(p[0],p1,p2);
        if(c==0) return p1.x<p2.x;
        return c>0;
    }
    point sta[N];
    int top;
    void convex(int n){
        top=0;
        sort(p,p+n,cmp1);
        sort(p+1,p+n,cmp2);
        sta[top++]=p[0];
        sta[top++]=p[1];
    
        for(int i=2;i<n;){
    
            if(top>1&&cross(sta[top-2],p[i],sta[top-1])>=0)top--;
            else {
                sta[top++]=p[i++];
            }
    
        }
    }
    
    int rotating()
    {
        int q=1;
        int ans=0;
       // cout<<"all:"<<top<<endl;
        sta[top]=sta[0];
        int j,k;
        j=1,k=2;
    
        for(int i=0;i<top;i++)
        {
            while(cross(sta[i],sta[j],sta[(k+1)%top])>cross(sta[i],sta[j],sta[k]))
                k=(k+1)%top;
            while(cross(sta[i],sta[(j+1)%top],sta[(k)%top])>cross(sta[i],sta[j],sta[k]))
                j=(j+1)%top;
    
            ans=max(ans,abs(cross(sta[i],sta[j],sta[k])));
        }
        cout<<fixed<<setprecision(2)  <<0.5*ans<<endl;
      /*  double t=13.444;
        cout<<fixed<<setprecision(0)<<t<<endl;
        cout<<t<<endl;
        cout<<fixed<<setprecision(2);
        cout<<t<<endl;*/
    }
    
    int main()
    {
        //freopen("cin.txt","r",stdin);
        int n;
        while(cin>>n&&n!=-1){
            for(int i=0;i<n;i++){
                scanf("%d%d",&p[i].x,&p[i].y);
            }
            convex(n);
            rotating();
          //  printf("%d
    ",rotating());
        }
        return 0;
    }
    

      








  • 相关阅读:
    Hibernate框架—简介
    ooad单例模式-Singleton
    About-JavaOOAD
    win10内置ubuntu,mysql完美清理,并安装
    VUE关于对象动态添加属性无法双向绑定问题
    正则留档
    centos下tomcat中文路径不识别
    web服务器解释html-include
    rose
    聊天框突出箭头
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11432425.html
Copyright © 2011-2022 走看看