题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670

用叉积判断。注意两端的平行于 y 轴的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define db double
using namespace std;
const int N=,INF=1e7;
int n,sta[N],top;db ans;
struct Node{
int x,y;
Node(int x=,int y=):x(x),y(y) {}
bool operator< (const Node &b)const
{return x<b.x||(x==b.x&&y<b.y);}
Node operator- (const Node &b)const
{return Node(b.x-x,b.y-y);}
}a[N];
ll Sqr(int x){return (ll)x*x;}
ll Cross(Node a,Node b){return (ll)a.x*b.y-(ll)a.y*b.x;}
db dist(Node a,Node b){return sqrt(Sqr(a.x-b.x)+Sqr(a.y-b.y));}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
sort(a+,a+n+);
for(int i=;i<=n;i++)
{
while(top>&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-]])>=)top--;
sta[++top]=i;
}
for(int i=;i<=top;i++)ans+=dist(a[sta[i-]],a[sta[i]]);
for(int i=;i<=n;i++)a[i].y=INF-a[i].y+;
sort(a+,a+n+);
top=;
for(int i=;i<=n;i++)
{
while(top>&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-]])>=)top--;
sta[++top]=i;
}
for(int i=;i<top;i++)ans+=dist(a[sta[i-]],a[sta[i]]);
if(top>&&a[sta[top]].x!=a[sta[top-]].x)ans+=dist(a[sta[top-]],a[sta[top]]);
if(a[].x==a[].x)ans+=abs(a[].y-a[].y);
printf("%.2f\n",ans);
return ;
}

bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包的更多相关文章

  1. bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Description 为了防止 ...

  2. BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘

    Description 求凸包周长. Sol 凸包+计算几何. 这好像叫什么 Graham Scan 算法... 这个可以求凸包的周长,直径,面积. 选择一个基点,然后按极角排序,最后用一个栈一直维护 ...

  3. 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)

    链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  4. bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】

    凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...

  5. 【BZOJ】1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1670 裸打了凸包.. #include <cstdio> #include <cs ...

  6. BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包

    BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...

  7. BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...

  8. 【计算几何】【凸包】bzoj1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...

  9. bzoj 1670 Building the Moat护城河的挖掘 —— 凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 单调栈维护凸包即可,用叉积判断: 维护上凸壳,然后把所有点的纵坐标翻转再求上凸壳即可, ...

随机推荐

  1. Valgrind查找内存泄露利器

    Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的ma ...

  2. Linux内建命令和外部命令

    Linux命令有内部命令(内建命令)和外部命令之分,内部命令和外部命令功能基本相同,但也有些细微差别. [内部命令 vs. 外部命令] (1)内部命令实际上是shell程序的一部分,其中包含的是一些比 ...

  3. day33 Python与金融量化分析(三)

    第三部分 实现简单的量化框架 框架内容: 开始时间.结束时间.现金.持仓数据 获取历史数据 交易函数 计算并绘制收益曲线 回测主体框架 计算各项指标 用户待写代码:初始化.每日处理函数 第四部分 在线 ...

  4. OGRE渲染流程

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/

  5. Spring源码解析-IOC容器的实现-ApplicationContext

    上面我们已经知道了IOC的建立的基本步骤了,我们就可以用编码的方式和IOC容器进行建立过程了.其实Spring已经为我们提供了很多实现,想必上面的简单扩展,如XMLBeanFacroty等.我们一般是 ...

  6. Algorithm4.子数组求和贪心

    子数组求和最大问题 20131011 问题描述 一个数组中,有整数也有复数,求这个数组的所有子数组中,求和最大的值. 这是一个动态规划问题,乍看上去没有什么简单的方法,把所有的情况列出来就可以了,但是 ...

  7. Java进阶3. 内存回收机制

    Java进阶3. 内存回收机制 20131029 前言: 学过C++的都知道,C++中内存需要程序员自己维护.说道这里,很多开发的同学就感觉很痛苦,当他转向Java的时候,就会说你看Java多好啊,程 ...

  8. 使用 Express 实现一个简单的 SPA 静态资源服务器

    背景 限制 SPA 应用已经成为主流,在项目开发阶段产品经理和后端开发同学经常要查看前端页面,下面就是我们团队常用的使用 express 搭建的 SPA 静态资源服务器方案. 为 SPA 应用添加入口 ...

  9. TableviewCell嵌套TextView自动换行

    TableviewCell嵌套TextView自动换行 欢迎到github上去下载源码Demo, github链接:https://github.com/mengzhihun6 效果图:

  10. 《Effective C++》第4章 设计与声明(1)-读书笔记

    章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...