题目

链接

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

分析

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

 #include<bits/stdc++.h>
using namespace std; struct Point{
double x, y;
Point(double x=, double y=):x(x), y(y){}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B){
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Point A, Point B){
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
double Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
} int ConvexHull(Point* p, int n, Point* ch)
{
sort(p, p+n);
int m=;
for(int i = ;i < n;i++)
{
while(m > && Cross(ch[m-] - ch[m-], p[i] - ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-;i >= ;i--)
{
while(m > k && Cross(ch[m-] - ch[m-], p[i] - ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
return m;
} const int maxn = + ;
int n;
Point points[maxn * ], ch[maxn * ]; int main()
{
//printf("%lf\n", sqrt(2) * 4);
while(scanf("%d", &n) == )
{
double x, y;
int cnt = ;
for(int i = ;i < n;i++)
{
scanf("%lf%lf", &x, &y);
points[cnt].x = x+, points[cnt++].y = y;
points[cnt].x = x-, points[cnt++].y = y;
points[cnt].x = x, points[cnt++].y = y+;
points[cnt].x = x, points[cnt++].y = y-;
}
int pcnt = ConvexHull(points, cnt, ch);
double ans = ; //sort(ch, ch + pcnt); //for(int i = 0;i < pcnt;i++) printf("%d: %lf %lf\n", i, ch[i].x, ch[i].y); double dx = fabs(ch[].x - ch[pcnt-].x);
double dy = fabs(ch[].y - ch[pcnt-].y);
if(dx > dy) swap(dx, dy);
ans += dx * sqrt();
ans += (dy - dx); for(int i = ;i < pcnt;i++)
{
double dx = fabs(ch[i].x - ch[i-].x);
double dy = fabs(ch[i].y - ch[i-].y);
if(dx > dy) swap(dx, dy);
ans += dx * sqrt();
ans += (dy - dx); //printf("%lf\n", ans);
}
printf("%lf\n", ans);
}
return ;
}

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

UVALive 6859——凸包&&周长的更多相关文章

  1. HDU 1392 凸包模板题,求凸包周长

    1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...

  2. poj 1113:Wall(计算几何,求凸包周长)

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28462   Accepted: 9498 Description ...

  3. Wall---hdu1348(求凸包周长 模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...

  4. POJ 1113 Wall(Graham求凸包周长)

    题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...

  5. HDU 1392 Surround the Trees (Graham求凸包周长)

    题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...

  6. poj 1113 凸包周长

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Descriptio ...

  7. LightOJ 1239 - Convex Fence 凸包周长

    LINK 题意:类似POJ的宫殿围墙那道,只不过这道题数据稍微强了一点,有共线的情况 思路:求凸包周长加一个圆周长 /** @Date : 2017-07-20 15:46:44 * @FileNam ...

  8. hdu 1392:Surround the Trees(计算几何,求凸包周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1348:Wall(计算几何,求凸包周长)

    Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. DataNode 详解及HDFS 2.X新特性

    1. 工作机制 一个数据块在 DataNode 上以文件形式存储在磁盘上,包括两个文件,一个是数据本身,一个是元数据包括数据块的长度,块数据的校验和,以及时间戳. DataNode 启动后向 Name ...

  2. ######【Python】【基础知识】【一些未知的知识点】######

    在查资料.看书过程中遇到的一些未知的领域.知识点: 1.模仿静态变量 可参考: https://www.runoob.com/python/python-exercise-example41.html ...

  3. python3.5+installer 将.py 打包成.exe

    (1)下载安装installer,不如我安装在D:\Program Files\Python35,安装完成后,在D:\Program Files\Python35\Scripts可以找到install ...

  4. k8s-日志收集架构

    日志收集 Kubernetes 集群中监控系统的搭建,除了对集群的监控报警之外,还有一项运维工作是非常重要的,那就是日志的收集. 介绍 应用程序和系统日志可以帮助我们了解集群内部的运行情况,日志对于我 ...

  5. vue之多页面的开发

    我们平常用vue开发的时候总觉得vue好像就是专门为了单页面应用而诞生的,其实不是.因为vue在工程化开发的时候很依赖webpack,而webpack是将所有的资源整合到一块,弄成一个单页面.但是vu ...

  6. 怎样快捷获取元素节点body

    1. 使用: document.body document.body.nodeName; // "BODY" 2. 使用: document.getElementsByTagNam ...

  7. hdu 6047

    题解:先对b排序,用一个数组预处理a,记录当前位置之后到n的最大值,然后在用一个变量维护新增变量的最大值,用的时候和前面的数组的最大值做一个比较就ok. AC代码: #include <cstd ...

  8. Python脚本:Linux自动化执行Python脚本

    1.环境及其工具: ubuntu 16.04 python2.7(自带) pip2.7(安装) virtualenv(安装) crontab (自带) 2.pip2.7安装 (1)尝试使用 sudo ...

  9. MVC部分视图的使用(Html.Partial/RenderPartial、Html.Action/RenderAction、RenderPage)

    ASP.NET MVC 里页面往往会有许多重用的地方,可以进行封装重用. 使用部分视图有以下优点: 1. 可以简写代码. 2. 页面代码更加清晰.更好维护. 在视图里有多种方法可以 加载部分视图,包括 ...

  10. Linux下mysql创建用户并设置权限,设置远程连接

    为了安全考虑,OneinStack仅允许云主机本机(localhost)连接数据库,如果需要远程连接数据库,需要如下操作:打开iptables 3306端口 # iptables -I INPUT 4 ...