zoukankan      html  css  js  c++  java
  • (几何)LeetCode Weekly Contest 32 D-Erect the Fence

    587. Erect the Fence

     
    • User Accepted: 78
    • User Tried: 196
    • Total Accepted: 82
    • Total Submissions: 452
    • Difficulty: Hard

    There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. Your job is to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

    Example 1:

    Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
    Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
    Explanation:
    

    Example 2:

    Input: [[1,2],[2,2],[4,2]]
    Output: [[1,2],[2,2],[4,2]]
    Explanation:
    
    Even you only have trees in a line, you need to use rope to enclose them. 
    

    Note:

    1. All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
    2. All input integers will range from 0 to 100.
    3. The garden has at least one tree.
    4. All coordinates are distinct.
    5. Input points have NO order. No order required for output.
     1 class Solution {
     2 public:
     3     /*计算两点之间距离*/
     4     static int dis(Point p1,Point p2)
     5     {
     6         return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
     7     }
     8     /*利用×乘 计算三点之间关系 0:共线 1:依次成顺时针 2:依次成逆时针*/
     9     static int orientation(Point p1,Point p2,Point p3)
    10     {
    11         int val=(p3.y-p1.y)*(p2.x-p1.x)-(p3.x-p1.x)*(p2.y-p1.y);
    12         if(val==0)
    13             return 0;
    14         else
    15             return val>0?2:1;
    16     }
    17     /*将点排序 采用结构体进行排序 第一次见*/
    18     struct pointsComparator
    19     {
    20         Point p0;/*基准点*/
    21         bool operator() (const Point& p1,const Point&  p2)
    22         {
    23             int val=orientation(p0,p1,p2);
    24             if(val==0)
    25                 return dis(p0,p1)<=dis(p0,p2);
    26             else
    27                 return val==2;
    28         }
    29         pointsComparator(Point p): p0(p){}
    30     };
    31     vector<Point> outerTrees(vector<Point>& points) {
    32         int n=points.size();
    33         if(n<=3)
    34             return points;
    35         int ymin=points[0].y,minlo=0;/*找y坐标最小的点,记录其下标*/
    36         for(int i=1;i<n;++i)
    37         {
    38             if(points[i].y<ymin||(points[i].y==ymin&&points[i].x<points[minlo].x))
    39                 ymin=points[i].y,minlo=i;
    40         }
    41         Point tem=points[0];
    42         points[0]=points[minlo];
    43         points[minlo]=tem;
    44         Point p0=points[0];
    45         sort(points.begin(),points.end(),pointsComparator(p0));
    46         
    47         
    48         Point pn = points.back();
    49         if (orientation(p0, points[1], pn) != 0) {//非所有点都共线
    50             int idx = n-1;
    51             while (orientation(p0, points[idx], pn) == 0) {//找到
    52                 idx--;
    53             }
    54             reverse(points.begin() + idx + 1, points.end());//调序??为啥
    55         }
    56         
    57         
    58         vector<Point> vertices;
    59         vertices.push_back(points[0]);
    60         vertices.push_back(points[1]);
    61         vertices.push_back(points[2]);
    62         for(int i=3;i<n;++i)
    63         {
    64             while(orientation(vertices[vertices.size()-2],vertices.back(),points[i])==1)
    65                 vertices.pop_back();
    66             vertices.push_back(points[i]);
    67         }
    68         return vertices;
    69     }
    70 };
  • 相关阅读:
    笔记:今天必须读完的文章
    windows android 第三方模拟器 看日志
    彻底搞懂Android文件存储---内部存储,外部存储以及各种存储路径解惑
    texturepacker命令行处理图片 格式选择
    Android插件化主流框架和实现原理
    Socket心跳包机制与实现 一般的应用下,判定时间在30-40秒比较不错。如果实在要求高,那就在6-9秒。
    图解:HTTP 范围请求,助力断点续传、多线程下载的核心原理
    localstorage的跨域存储方案 介绍
    UGUI的图集处理方式-SpriteAtlas的前世今生
    web自动化,下拉滚动到底部/顶部和下拉滚动到指定的元素
  • 原文地址:https://www.cnblogs.com/quintessence/p/6852902.html
Copyright © 2011-2022 走看看