链接

luogu

模板一

上下利用斜率求凸包然后合并。

#include <bits/stdc++.h>
using namespace std;
const int N=10005;
const double eps=1e-10,inf=0x3f3f3f3f3f3f3f3f;
int n,stak[N],top;
struct point {
double x,y;
}a[N];
bool cmp(point a,point b) {
return a.x==b.x?a.y<b.y:a.x<b.x;
}
double dis(point a,point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double get_k(point a,point b) {
return a.x==b.x?inf:(a.y-b.y)/(a.x-b.x);
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%lf%lf",&a[i].x,&a[i].y);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;++i) {
while(top>=2&&get_k(a[stak[top-1]],a[stak[top]])<get_k(a[stak[top]],a[i])) top--;
stak[++top]=i;
}
double ans=0;
for(int i=1;i<top;++i) ans+=dis(a[stak[i]],a[stak[i+1]]);
top=0;
for(int i=n;i>=1;--i) {
while(top>=2&&get_k(a[stak[top-1]],a[stak[top]])<get_k(a[stak[top]],a[i])) top--;
stak[++top]=i;
}
for(int i=1;i<top;++i) ans+=dis(a[stak[i]],a[stak[i+1]]);
printf("%.2lf",ans);
return 0;
}

模板2

Graham算法

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+6;
const double eps=1e-10;
struct point {
double x,y;
}P[N],tmp[N];
point operator - (point a,point b) {
return (point){a.x-b.x,a.y-b.y};
}
point operator + (point a,point b) {
return (point){a.x+b.x,a.y+b.y};
}
double operator * (point a,point b) {
return a.x*b.y-a.y*b.x;
}
int n,stak[N],top;
double dis(point a,point b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool judge_onleft(int p0,int p1,int p2) {
double s=(P[p1]-P[p0])*(P[p2]-P[p0]);
return s<0||((s==0&&dis(P[p1],P[p0]))>=dis(P[p2],P[p0]));
}
bool cmp(point a,point b) {
double s=(a-P[1])*(b-P[1]);
return s<0||(s==0&&dis(a,P[1])>=dis(b,P[1]));
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%lf%lf",&P[i].x,&P[i].y);
int First=1;
for(int i=2;i<=n;++i)
if(P[i].x<P[First].x||(P[i].x==P[First].x&&P[i].y<P[First].y)) First=i;
swap(P[First],P[1]);
sort(P+2,P+1+n,cmp);
P[n+1]=P[1];
stak[1]=1,stak[2]=2;
top=2;
for(int i=3;i<=n+1;++i) {
while(top>1&&judge_onleft(stak[top-1],i,stak[top])) top--;
stak[++top]=i;
}
top--;
double ans=0;
for(int i=1;i<top;++i) ans+=sqrt(dis(P[stak[i]],P[stak[i+1]]));
ans+=sqrt(dis(P[stak[top]],P[stak[1]]));
printf("%.2lf",ans);
return 0;
}

luogu 2742 二维凸包的更多相关文章

  1. Luogu P2742 模板-二维凸包

    Luogu P2742 模板-二维凸包 之前写的实在是太蠢了.于是重新写了一个. 用 \(Graham\) 算法求凸包. 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 ...

  2. luogu P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

    题解: 二维凸包裸题 按照x坐标为第一关键字,y坐标为第二关键字排序 然后相邻判断叉积用单调队列搞过去 正反都做一次就好了 代码: #include <bits/stdc++.h> usi ...

  3. 使用Graham扫描法求二维凸包的一个程序

    #include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...

  4. 【洛谷 P2742】【模板】二维凸包

    题目链接 二维凸包板子..有时间会补总结的. #include <cstdio> #include <cmath> #include <algorithm> usi ...

  5. poj 2079 Triangle (二维凸包旋转卡壳)

    Triangle Time Limit: 3000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Submit Stat ...

  6. poj 2187 Beauty Contest(二维凸包旋转卡壳)

    D - Beauty Contest Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  7. UVA 10652 Board Wrapping(二维凸包)

    传送门 刘汝佳<算法竞赛入门经典>P272例题6包装木板 题意:有n块矩形木板,你的任务是用一个面积尽量小的凸多边形把它们抱起来,并计算出木板占整个包装面积的百分比. 输入:t组数据,每组 ...

  8. 【计算几何】二维凸包——Graham's Scan法

    凸包 点集Q的凸包(convex hull)是指一个最小凸多边形,满足Q中的点或者在多边形边上或者在其内.右图中由红色线段表示的多边形就是点集Q={p0,p1,...p12}的凸包. 一组平面上的点, ...

  9. 计算几何 二维凸包问题 Andrew算法

    凸包:把给定点包围在内部的.面积最小的凸多边形. Andrew算法是Graham算法的变种,速度更快稳定性也更好. 首先把全部点排序.依照第一keywordx第二keywordy从小到大排序,删除反复 ...

随机推荐

  1. Unable to connect to HBase using Phoenix JDBC Driver

    Feb 01, 2017; 5:21pm Unable to connect to HBase using Phoenix JDBC Driver 9 posts Hi All,   I am try ...

  2. intel AVX指令集

    听说这 AVX 很牛,,支持Win7,大幅提高游戏浮点运算性能warning C4752: 发现 Intel(R) 高级矢量扩展:请考虑使用 /arch:AVX

  3. 象棋中“车”的攻击范围_Java

    代码如下: String[][] a = new String[8][8]; int h, l; Scanner scan = new Scanner(System.in); System.out.p ...

  4. Spring Boot 复习

    简介 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭 建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义 ...

  5. docker下安装redis集群

    docker-compose.yml master: image: redis:4 container_name: redis-cluster_master command: redis-server ...

  6. QT之Qt之Q_PROPERTY宏理解

    在初学Qt的过程中,时不时地要通过F2快捷键来查看QT类的定义,发现类定义中有许多Q_PROPERTY的东西,比如最常用的QWidget的类定义: Qt中的Q_PROPERTY宏在Qt中是很常用的,那 ...

  7. input限制输入

    input 只能输入数字.字母.汉字等   1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace( ...

  8. vagrant 无法挂载共享目录

    Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem " ...

  9. python实现系统调用cmd命令的模块---subprocess模块

    如果要python实现系统命令或者调用脚本,python中可以利用os或者subprocess模块实现: 一.os模块: # coding:utf-8 command = os.system('net ...

  10. Mac pro操作快捷键

    1. 在Finder顶部显示文件/文件夹全路径 终端里输入:defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;kil ...