• UVALive 6859——凸包&&周长


    题目

    链接

    题意:在一个网格图上,给出$n$个点的坐标,用一个多边形包围这些点(不能接触,且多边形的边只能是对角线或直线),求多边形的最小周长.

    分析

    对于每个点,我们考虑与之相邻的4个点。一共由 $4  imes N$ 个点,然后求凸包。对凸包上每对相邻的点,优先走对角线,然后走直线。累加长度即可。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct Point{
     5     double x, y;
     6     Point(double x=0, double y=0):x(x), y(y){}
     7 };
     8 typedef Point Vector;
     9 Vector operator + (Vector A, Vector B){
    10     return Vector(A.x+B.x, A.y+B.y);
    11 }
    12 Vector operator - (Point A, Point B){
    13     return Vector(A.x-B.x, A.y-B.y);
    14 }
    15 Vector operator * (Vector A, double p){
    16     return Vector(A.x * p, A.y * p);
    17 }
    18 Vector operator / (Vector A, double p){
    19     return Vector(A.x / p, A.y / p);
    20 }
    21 bool operator < (const Point& a, const Point& b){
    22     return a.x < b.x || (a.x == b.x && a.y < b.y);
    23 }
    24 double Cross(Vector A, Vector B)
    25 {
    26     return A.x * B.y - A.y * B.x;
    27 }
    28 
    29 int ConvexHull(Point* p, int n, Point* ch)
    30 {
    31     sort(p, p+n);
    32     int m=0;
    33     for(int i = 0;i < n;i++)
    34     {
    35         while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)  m--;
    36         ch[m++] = p[i];
    37     }
    38     int k = m;
    39     for(int i = n-2;i >= 0;i--)
    40     {
    41         while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)  m--;
    42         ch[m++] = p[i];
    43     }
    44     if(n > 1)  m--;
    45     return m;
    46 }
    47 
    48 const int maxn = 100000 + 10;
    49 int n;
    50 Point points[maxn * 4], ch[maxn * 4];
    51 
    52 int main()
    53 {
    54     //printf("%lf
    ", sqrt(2) * 4);
    55     while(scanf("%d", &n) == 1)
    56     {
    57         double x, y;
    58         int cnt = 0;
    59         for(int i = 0;i < n;i++)
    60         {
    61             scanf("%lf%lf", &x, &y);
    62             points[cnt].x = x+1, points[cnt++].y = y;
    63             points[cnt].x = x-1, points[cnt++].y = y;
    64             points[cnt].x = x, points[cnt++].y = y+1;
    65             points[cnt].x = x, points[cnt++].y = y-1;
    66         }
    67         int pcnt = ConvexHull(points, cnt, ch);
    68         double ans = 0;
    69 
    70         //sort(ch, ch + pcnt);
    71 
    72         //for(int i = 0;i < pcnt;i++)  printf("%d: %lf  %lf
    ", i, ch[i].x, ch[i].y);
    73 
    74         double dx = fabs(ch[0].x - ch[pcnt-1].x);
    75         double dy = fabs(ch[0].y - ch[pcnt-1].y);
    76         if(dx > dy)  swap(dx, dy);
    77         ans += dx * sqrt(2);
    78         ans += (dy - dx);
    79 
    80         for(int i = 1;i < pcnt;i++)
    81         {
    82             double dx = fabs(ch[i].x - ch[i-1].x);
    83             double dy = fabs(ch[i].y - ch[i-1].y);
    84             if(dx > dy)  swap(dx, dy);
    85             ans += dx * sqrt(2);
    86             ans += (dy - dx);
    87 
    88             //printf("%lf
    ", ans);
    89         }
    90         printf("%lf
    ", ans);
    91     }
    92     return 0;
    93 }

    参考链接:https://blog.csdn.net/qingshui23/article/details/52809736

  • 相关阅读:
    redis 五种数据结构详解(string,list,set,zset,hash)
    推荐一个同步Mysql数据到Elasticsearch的工具
    一些经验,用来鉴别不太靠谱的公司或工作(面试是双向的,是你最好的了解这个公司的机会)
    OpenSSL 使用 base64 编码/解码(liang19890820)
    Qt之QEvent(所有事件的翻译)
    Go 在 Windows 上用户图形界面 GUI 解决方案 Go-WinGUI 国产(使用cef 内核)
    卷积神经网络CNN
    Event Driven Architecture
    wineshark分析抓取本地回环包
    僵尸进程与孤儿进程
  • 原文地址:https://www.cnblogs.com/lfri/p/11181967.html
Copyright © 2020-2023  润新知