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

      








  • 相关阅读:
    示例vue 的keep-alive缓存功能的实现
    解析Vue.js中的computed工作原理
    CentOS7.2 问题收集 查看文件大小 查看端口
    Docker 配置阿里云镜像加速器
    CentOS7.2中systemctl的使用
    CentOS7.2 安装Docker
    Java 多线程中的任务分解机制-ForkJoinPool,以及CompletableFuture
    IntelliJ IDEA 在运行web项目时部署的位置
    Mysql相关问题收集
    Java命令使用 jmap,jps,jstack,jstat,jhat,jinfo
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11432425.html
Copyright © 2011-2022 走看看