UVALive 6859——凸包&&周长

题目

链接

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

分析

对于每个点,我们考虑与之相邻的4个点。一共由 $4  \times 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\n", 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\n", 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\n", ans);
89         }
90         printf("%lf\n", ans);
91     }
92     return 0;
93 }

 

 

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

 

转载于:https://www.cnblogs.com/lfri/p/11181967.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值