题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1348

凸包模板:

const int N =1010;
const double PI = 3.1415927;
double EPS=1e-10;
// 考虑误差的加法运算
double add(double a,double b)
{if(fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0;return a+b;
}
struct Point{double x,y;Point(){}Point(double x,double y):x(x),y(y){} // 构造函数,方便代码编写Point(const Point & p):x(p.x),y(p.y){}Point operator +(Point p){return Point(add(x,p.x), add(y,p.y));}Point operator-(Point p){return Point(add(x,-p.x),add(y,-p.y));}Point operator*(double d){return Point(x*d,y*d);}double operator*(Point p){  // 内积 点乘return add(x*p.x, y*p.y);}double operator^(Point p){//  外积 叉乘return add(x*p.y,-y*p.x);}friend ostream& operator<<(ostream& os,const Point& p ){os<<p.x<<" "<<p.y<<endl;return os;}friend istream& operator>>(istream& is, Point& rh) {// Point 不能是常量,是变量is>>rh.x>>rh.y;return is;}double dist(Point p){return sqrt( add( (x-p.x)*(x-p.x),(y-p.y)* (y-p.y) ) );}
};Point List[N];   // 输入点集Q
Point stack[N];  // 栈从低到顶部包含了按逆时针方向排列在CH(Q)(凸包)中的各个顶点。
int top;   // 栈顶bool cmp(Point a,Point b)
{if(a.y!= b.y)return a.y<b.y;else return a.x<b.x;
}
// 按极角排序,如果极角相等则按距离从小到大,sort是按从小到大排列的,故需对<符号重载
bool operator<(Point p1,Point p2){double  tmp=(p1-List[0])^(p2-List[0]); //List[0]为基点if(tmp>0) return 1;else if(tmp==0 && p1.dist(List[0])< p2.dist(List[0])) return 1;else return 0;}// 输入点集Q,并把最左下方的点放在 LIst[0],作为基点,
//并且进行极角排序,对sort(list+1,List+n) ,最大时间O(nlgn)
void init(int n)
{for(int i=1;i<n;i++)cin>>List[i];sort(List, List+n,cmp); // List[0] 存储的是 左下方的点sort(List+1,List+n);   // 对 List[1]~ List[n-1] 进行对 List[0]的极角排序
}
// 寻找凸包 graham 扫描法 时间O(n)
void graham(int n)
{if(n==1){top=0;stack[0]=List[0];}if(n==2){top=1;stack[0]=List[0];stack[1]=List[1];}if(n>2){for(int i=0;i<=1;i++)  // 初始化栈,有p0,p1进栈stack[i]=List[i];top=1;for(int i=2;i<n;i++){while(top>=1 && ((stack[top]-stack[top-1])^(List[i]-stack[top-1]))<=0) // 非左转top--; // 删除栈顶元素top++;stack[top]=List[i]; // 将i压入栈
        }}
}

hdu 1348 求凸包的周长 + 圆周长

http://acm.hdu.edu.cn/showproblem.php?pid=1348

代码如下:

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <math.h>
#include <cmath>
#include <map>
#include <queue>using namespace std;
typedef long long ll;const int N =1010;
const double PI = 3.1415927;
double EPS=1e-10;
// 考虑误差的加法运算
double add(double a,double b)
{if(fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0;return a+b;
}
struct Point{double x,y;Point(){}Point(double x,double y):x(x),y(y){} // 构造函数,方便代码编写Point(const Point & p):x(p.x),y(p.y){}Point operator +(Point p){return Point(add(x,p.x), add(y,p.y));}Point operator-(Point p){return Point(add(x,-p.x),add(y,-p.y));}Point operator*(double d){return Point(x*d,y*d);}double operator*(Point p){  // 内积 点乘return add(x*p.x, y*p.y);}double operator^(Point p){//  外积 叉乘return add(x*p.y,-y*p.x);}friend ostream& operator<<(ostream& os,const Point& p ){os<<p.x<<" "<<p.y<<endl;return os;}friend istream& operator>>(istream& is, Point& rh) {// Point 不能是常量,是变量is>>rh.x>>rh.y;return is;}double dist(Point p){return sqrt( add( (x-p.x)*(x-p.x),(y-p.y)* (y-p.y) ) );}
};Point List[N];   // 输入点集Q
Point stack[N];  // 栈从低到顶部包含了按逆时针方向排列在CH(Q)(凸包)中的各个顶点。
int top;   // 栈顶bool cmp(Point a,Point b)
{if(a.y!= b.y)return a.y<b.y;else return a.x<b.x;
}
// 按极角排序,如果极角相等则按距离从小到大,sort是按从小到大排列的,故需对<符号重载
bool operator<(Point p1,Point p2){double  tmp=(p1-List[0])^(p2-List[0]); //List[0]为基点if(tmp>0) return 1;else if(tmp==0 && p1.dist(List[0])< p2.dist(List[0])) return 1;else return 0;}// 输入点集Q,并把最左下方的点放在 LIst[0],作为基点,
//并且进行极角排序,对sort(list+1,List+n) ,最大时间O(nlgn)
void init(int n)
{for(int i=0;i<n;i++)cin>>List[i];sort(List, List+n,cmp); // List[0] 存储的是 左下方的点sort(List+1,List+n);   // 对 List[1]~ List[n-1] 进行对 List[0]的极角排序
}
// 寻找凸包 graham 扫描法 时间O(n)
void graham(int n)
{if(n==1){top=0;stack[0]=List[0];}if(n==2){top=1;stack[0]=List[0];stack[1]=List[1];}if(n>2){for(int i=0;i<=1;i++)  // 初始化栈,有p0,p1进栈stack[i]=List[i];top=1;for(int i=2;i<n;i++){while(top>=1 && ((stack[top]-stack[top-1])^(List[i]-stack[top-1]))<=0) // 非左转top--; // 删除栈顶元素top++;stack[top]=List[i]; // 将i压入栈
        }}
}
int main()
{int T,count=0,n;double r;cin>>T;while(T--){if(count) cout<<endl;count=1;cin>>n>>r;init(n);graham(n);double ans=0;for(int i=0;i<top;i++)ans+=stack[i].dist(stack[i+1]);ans+=stack[top].dist(stack[0]);ans+=2*PI*r;printf("%.0lf\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/zn505119020/p/3623079.html

二维凸包(模板) hdu 1348 求凸包的周长相关推荐

  1. P2163 [SHOI2007]园丁的烦恼(二维数点模板题)

    P2163 [SHOI2007]园丁的烦恼 题意: 在一个二维平面内有一些点,给你一个左上角和右下角的点,问这个范围内有多少点 题解: 二维数点模板题 我们设F(a,b)表示以(0,0)为左下角,(a ...

  2. 凸包matlab代码,matlab求凸包

    实验结果需要一定的后处理,求凸包.matlab以专门的函数,查了半天也只能做到先用了,各个参数还不是很明白,先做记录.我是对一幅二值图像做凸包的,二值图如下: 然后用代码求出其凸包及最小外接矩形,代码 ...

  3. java二维数组周边元素_求出二维数组主对角线、次对角线以及周边元素之和

    某个同学的题目,写了一下. 题目大概是这样的: 编写函数,求出二维数组主对角线.次对角线以及周边元素之和. 要求:二维数组的行数.列数.数组元素在main函数中由键盘输入. #include int ...

  4. 凸包与Graham扫描法求凸包

    前置芝士 凸多边形 凸多边形指的是所有内角都在 (0,π](0,\pi](0,π] 范围内的多边形. 凸包 对于一个平面上的一个给定的点集,他们对应的凸包就是能把他们全部包含的最小凸多边形. 如果形象 ...

  5. c语言二维数组错误语法,关于c语言动态分配二维数组free的错误求dalao看看怎么回事谢谢啊~~~~...

    typedef struct { int**data; int row; int col; }MyMatrix; typedef MyMatrix* Matrix; /* 编写矩阵输入函数 INPUT ...

  6. matlab二维数组最小值出错,矩阵求最小值问题 问题是: 错误使用空矩形矩阵进行赋值...

    我现在有一组数据  当然其实是有很多数据了 已经上万 只截取了其中一部分 假设 [X Y W]=32.8876000000000        26.6148000000000             ...

  7. 若在矩阵A中存在一个元素Aij,该元素是第i行元素中最大值并且又是第j列元素中最小值,则称此元素值为该元素的一个鞍点。假设以二维数组存储矩阵A,求该矩阵中的所有鞍点。

    #include <stdio.h> #include <stdlib.h> int main() {     int nh,nl,max,p,top=0;//max存储该行的 ...

  8. [洛谷P2742]【模板】二维凸包([USACO5.1]圈奶牛Fencing the Cows)

    题目大意:求一个点集凸包边长 题解:求凸包,直接求 卡点:发现在较后面数位上有较小的误差,还以为是浮点数误差,最后发现是构造函数写成了$int$类型 C++ Code: #include <al ...

  9. 求最小子数组之二维篇

    一.设计思路 求出该二维数组的所有子数组,先确定一个位置为起点,然后向右下方依次以此起点为始的所有子数组, 图1-顺序求子数组 具体如上图1,顺序求出子数组,然后和max值相比较,若比max值大,则将 ...

最新文章

  1. matplotlib安装成功但import失败_统信UOS安装失败,deepinv20.1国产操作系统安装成功...
  2. Ubuntu开机自启动与sh脚本
  3. ios怎么安装python3.7_Python3、PyCharm的安装及使用方法(Mac版)
  4. zabbix mysql复制延迟_mysql 主从复制延迟监控
  5. JavaFX 的 UI 控件集 ControlsFX
  6. 数字音视频知识点汇总(二)
  7. springMVC与RESTful支持
  8. 如何使用VideoProc将MKV转换为MP4?
  9. Unity3d 枚举某个目录下所有资源
  10. 500状态码_教你玩转HTTP—状态码
  11. kafka分布式集群的操作
  12. Python语法练习
  13. 宝塔服务器性能跑分排行榜(CPU/内存/系统)
  14. 制作自己的微信小程序要怎么做?
  15. 阿里云镜像服务 vpc地址 无法 pull
  16. 创建个人网页,创建个人网址。
  17. 声声不息,新“声”报到
  18. stormzhang的自我介绍
  19. DevEco IDE 华为全系列远程真机免费调测
  20. 及时止损真的正确吗?

热门文章

  1. 扬州打工人租房编年史
  2. PAT甲级1054 map的使用
  3. mysql 单表多级查询_mysql单表与多表查询
  4. messagebox弹窗_从案例入手学Python——检测文件生成并弹窗提醒
  5. php 防止access token过期,微信調用接口,防止Access_token過期的方法
  6. mysql s索引 树_mysql 学习 - B+树索引
  7. lesson 4 Show Messages in Messagebox
  8. 基于深度学习的番茄叶片分割算法在手机上的应用(GDL+复杂背景只分割叶片有意义?)
  9. css背景图毛玻璃,css实现背景图片的毛玻璃效果
  10. CNN提取文本特征,融合PMF模型实现推荐系统