zoukankan      html  css  js  c++  java
  • 二维凸包模板

    NYOJ 78

    圈水池

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!(篱笆足够多,并且长度可变)
     
    输入
    第一行输入的是N,代表用N组测试数据(1<=N<=10) 第二行输入的是m,代表本组测试数据共有m个供水装置(3<=m<=100) 接下来m行代表的是各个供水装置的横纵坐标
    输出
    输出各个篱笆经过各个供水装置的坐标点,并且按照x轴坐标值从小到大输出,如果x轴坐标值相同,再安照y轴坐标值从小到大输出
    样例输入
    1
    4
    0 0
    1 1
    2 3
    3 0
    
    样例输出
    0 0
    2 3
    3 0
    
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    
    #define EPS 1e-8
    #define N 110
    
    int dump(double x)
    {
        if(fabs(x)<EPS) return 0;
        return x<0?-1:1;
    }
    struct Point
    {
        double x,y;
        Point(){}
        Point (double x,double y):x(x),y(y){
        }
        Point operator -(Point p){
            return    Point(x-p.x,y-p.y);
        }
        double operator ^ (Point p){
            return x*p.y-y*p.x;
        }
        bool operator < (const Point &p)const
        {
            if(x!=p.x) return x<p.x;
            return y<p.y;
        }
    };
    
    int n;
    Point p[N],q[N];
    
    int main()
    {
        int T,i;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                scanf("%lf%lf",&p[i].x,&p[i].y);
            }
            sort(p+1,p+n+1);
            int k=0;
            for(i=1;i<=n;i++) 
            {
                while(k>1 && dump((q[k]-q[k-1])^(p[i]-q[k-1]))<=0)
                    k--;
                q[++k]=p[i];
            }
            int t=k;
            for(i=n;i>=1;i--) 
            {
                while(k>t && dump((q[k]-q[k-1])^(p[i]-q[k-1]))<=0)
                    k--;
                q[++k]=p[i];
            }
            k-=1; 
            sort(q+1,q+k+1);
            for(i=1;i<=k;i++)
            {
                cout<<q[i].x<<' '<<q[i].y<<endl;
            }
        }
        return 0;
    }        

    POJ 1113

    Wall
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 30372   Accepted: 10218

    Description

    Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. 
    Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. 
    The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

    Input

    The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle. 
    Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

    Output

    Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

    Sample Input

    9 100
    200 400
    300 400
    300 300
    400 300
    400 400
    500 400
    500 200
    350 200
    200 200

    Sample Output

    1628

    Hint

    结果四舍五入就可以了
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    
    #define PI acos(-1.0)
    #define EPS 1e-8
    #define N 100010
    
    int dump(double x)
    {
       if(fabs(x)<EPS) return 0; return x<0?-1:1; } struct Point { double x,y; Point(){} Point (double x,double y):x(x),y(y){ } Point operator -(Point p){ return Point(x-p.x,y-p.y); } double operator ^ (Point p){ return x*p.y-y*p.x; } bool operator < (const Point &p)const { if(x!=p.x) return x<p.x; return y<p.y; } }; int n,r; Point p[N],q[N]; double PointDisPoint(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() { int i; while(scanf("%d%d",&n,&r)!=EOF) { for(i=1;i<=n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } sort(p+1,p+n+1); int k=0; for(i=1;i<=n;i++) { while(k>1 && dump((q[k]-q[k-1])^(p[i]-q[k-1]))<=0) k--; q[++k]=p[i]; } int t=k; for(i=n;i>=1;i--) { while(k>t && dump((q[k]-q[k-1])^(p[i]-q[k-1]))<=0) k--; q[++k]=p[i]; } double sum=0; for(i=2;i<=k;i++) { sum+=PointDisPoint(q[i-1],q[i]); } printf("%.0f ",sum+2*PI*(double)r); } return 0; }

    swustoj 249

    凸包面积

    麦兜是个淘气的孩子。一天,他在玩钢笔的时候把墨水洒在了白色的墙上。再过一会,麦兜妈就要回来了,麦兜为了不让妈妈知道这件事情,就想用一个白色的凸多边形把墙上的墨点盖住。你能告诉麦兜最小需要面积多大的凸多边形才能把这些墨点盖住吗? 
    现在,给出了这些墨点的坐标,请帮助麦兜计算出覆盖这些墨点的最小凸多边形的面积。

    多组测试数据。第一行是一个整数T,表明一共有T组测试数据。 
    每组测试数据的第一行是一个正整数N(0< N < = 105),表明了墨点的数量。接下来的N行每行包含了两个整数Xi和Yi(0<=Xi,Yi<=2000),表示每个墨点的坐标。每行的坐标间可能包含多个空格。

    每行输出一组测试数据的结果,只需输出最小凸多边形的面积。面积是个实数,小数点后面保留一位即可,不需要多余的空格。

    Input

    2
    4
    0 0
    1 0
    0 1
    1 1
    2
    0 0
    0 1
    Output
    1.0
    0.0
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    using namespace std;
     
    #define EPS 1e-8
    #define N 110
     
    int dump(double x)
    {
        if(fabs(x)<EPS) return 0;
        return x<0?-1:1;
    }
    struct Point
    {
        double x,y;
        Point(){}
        Point (double x,double y):x(x),y(y){
        }
        Point operator -(Point p){
            return  Point(x-p.x,y-p.y);
        }
        double operator ^ (Point p){
            return x*p.y-y*p.x;
        }
        bool operator < (const Point &p)const
        {
            if(x!=p.x) return x<p.x;
            return y<p.y;
        }
    };
     
    int n;
    Point p[N],q[N];
     
    int main()
    {
        int T,i;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                scanf("%lf%lf",&p[i].x,&p[i].y);
            }
            sort(p+1,p+n+1);
            int k=0;
            for(i=1;i<=n;i++)        
            {
                while(k>1 && dump((q[k]-q[k-1])^(p[i]-q[k-1]))<=0)
                    k--;
                q[++k]=p[i];
            }
            int t=k;
            for(i=n;i>=1;i--)     
            {
                while(k>t && dump((q[k]-q[k-1])^(p[i]-q[k-1]))<=0)
                    k--;
                q[++k]=p[i];
            }
            k-=1;                     
            double ans=0;
            for(i=3;i<=k;i++)
            {
                ans+=(q[i]-q[1])^(q[i-1]-q[0]);
            }
            printf("%.1f
    ",fabs(ans/2));
        }
        return 0;
    }

    POJ 2187

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 28628   Accepted: 8890

    Description

    Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 
    Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. 

    Input

    * Line 1: A single integer, N 
    * Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm 

    Output

    * Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

    Sample Input

    4
    0 0
    0 1
    1 1
    1 0
    

    Sample Output

    2
    

    Hint

    Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

     

    最远对距离

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    #define N 100010
    
    struct Point
    {
        int x,y;
        Point(){}
        Point (int x,int y):x(x),y(y){
        }
        Point operator -(Point p){
            return    Point(x-p.x,y-p.y);
        }
        int operator ^ (Point p){
            return x*p.y-y*p.x;
        }
        bool operator < (const Point &p)const
        {
            if(x!=p.x) return x<p.x;
            return y<p.y;
        }
    };
    
    int n;
    Point p[N],q[N];
    
    int main()
    {
        int i,j;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        sort(p+1,p+n+1);
        int k=0;
        for(i=1;i<=n;i++)
        {
            while(k>1 && ((q[k]-q[k-1])^(p[i]-q[k-1]))<=0)
                k--;
            q[++k]=p[i];
        }
        int t=k;
        for(i=n;i>=1;i--)
        {
            while(k>t && ((q[k]-q[k-1])^(p[i]-q[k-1]))<=0)
                k--;
            q[++k]=p[i];
        }
        k-=1;
        int ans=-1;
        for(i=1;i<=k-1;i++)
        {
            for(j=i+1;j<=k;j++)
            {
                ans=max(ans,(q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));
            }
        }
        cout<<ans<<endl;
        return 0;
    }

     

    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    android 之MediaPlayer MP3播放,VideoView 视频播放,MediaRecorder 录音
    IPHONE 开发 4 iPhone应用程序目录构成,工程项目的构成
    iPhone交流
    UILabel 用法
    跨网段 访问
    IPHONE 开发 8 Object C Foundation.h它包括所有的类 方法 集合,(id)init,iphone上使用Sqlite的注意事项小结
    查看与某一个表相关的视图、存储过程、函数
    IPHONE 开发 1 体系介绍
    android 之手机拨号器,以及短信发送器的简单实现
    Linux命令整理
  • 原文地址:https://www.cnblogs.com/hate13/p/4143539.html
Copyright © 2011-2022 走看看