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;
    }
    

      








  • 相关阅读:
    poj 1562 Oil Deposits
    poj 1650 Integer Approximation
    snmp4j 编程
    ubuntu 13.04 163源(亲测可用)
    c语言中static 用法总结(转)
    Spring入门
    Hibernate入门
    Struts2入门教程
    素数距离问题
    ASCII码排序
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11432425.html
Copyright © 2011-2022 走看看