Luogu P2742 模板-二维凸包

  • 之前写的实在是太蠢了.于是重新写了一个.
  • 用 \(Graham\) 算法求凸包.
  • 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 \(a\) 的左侧,于是可以用这个方法判断是否弹点.
  • 写的时候注意细节:确定原点时的比较和排序时的比较是不同的,并且排序时不要把原点加入.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int MAXN=1e4+10;
int n;
struct v2
{
double x,y;
v2(double x=0,double y=0):x(x),y(y) {}
friend double operator * (const v2 &a,const v2 &b)
{
return a.x*b.y-a.y*b.x;
}
friend v2 operator + (const v2 &a,const v2 &b)
{
return v2(a.x+b.x,a.y+b.y);
}
friend v2 operator - (const v2 &a,const v2 &b)
{
return v2(a.x-b.x,a.y-b.y);
}
bool operator < (const v2 &rhs) const
{
return x==rhs.x?y<rhs.y:x<rhs.x;
}
double modulus()
{
return sqrt(x*x+y*y);
}
double angle()
{
return atan2(y,x);
}
}p[MAXN];
v2 origin;
bool cmp(const v2 &a,const v2 &b)
{
double a1=(a-origin).angle();
double a2=(b-origin).angle();
return a1==a2?a.x<b.x:a1<a2;
}
v2 stk[MAXN];
int tp=0;
void ConvexHull()
{
for(int i=2;i<=n;++i)
if(p[i]<p[1])
swap(p[i],p[1]);
origin=p[1];
sort(p+2,p+n+1,cmp);
for(int i=1;i<=n;++i)
{
while(tp>=2 && (stk[tp]-stk[tp-1])*(p[i]-stk[tp])<=0)
--tp;
stk[++tp]=p[i];
}
stk[++tp]=p[1];
}
double calcC()
{
double s=0;
for(int i=1;i<tp;++i)
s+=(stk[i+1]-stk[i]).modulus();
return s;
}
int main()
{
n=read();
for(int i=1;i<=n;++i)
{
double x,y;
scanf("%lf%lf",&x,&y);
p[i]=v2(x,y);
}
ConvexHull();
double ans=calcC();
printf("%.2lf\n",ans);
return 0;
}

最新文章

  1. sublime text保存时删除行尾空格
  2. Ruby on Rails vs. PHP vs. Python
  3. (转)Hprose与WCF在云计算平台Azure上的对决
  4. android自定义控件实现刮刮乐效果
  5. Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十二)
  6. 如何在MFC中操作资源句柄
  7. 关于埃博拉(Ebola)基础研究病毒
  8. Linux最小化安装
  9. 第一次app经验
  10. 两段锁协议(Two-Phase Locking――2PL)
  11. Service里边启动Activity注意事项
  12. Executors提供的四种线程池
  13. PCA算法Python实现
  14. 现代OpenGL渲染管线介绍
  15. Android(三) 匹配屏幕密度
  16. 跨域ajax请求携带cookie
  17. Android API之android.provider.ContactsContract
  18. Java NIO使用及原理分析 (四)(转)
  19. HTML5基础实例(三)
  20. iOS 开发经验总结

热门文章

  1. docker-machine windows
  2. 洛谷P3601 签到题
  3. 前端项目,引入PingFang SC字体
  4. PHP中用下划线开头的含义
  5. customs event
  6. Android Kotlin —— 语言结合
  7. Codeforces Round #424
  8. Python之NumPy(axis=0 与axis=1)区分
  9. Python 序列化pickle/cPickle模块整理
  10. Linux中jar包指定端口启动并记录日志