Description

为了防止口渴的食蚁兽进入他的农场,$Farmer John$决定在他的农场周围挖一条护城河。农场里一共有$N$股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水。护城河必须能保护所有的泉水,也就是说,能包围所有的泉水。泉水一定在护城河的内部,或者恰好在河道上。当然,护城河构成一个封闭的环。 挖护城河是一项昂贵的工程,于是,节约的$FJ$希望护城河的总长度尽量小。请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少。

所有泉水的坐标都在范围为$(1..10^7,1..10^7)$的整点上,一股泉水对应着一个唯一确定的坐标。并且,任意三股泉水都不在一条直线上。

以下是一幅包含$20$股泉水的地图,泉水用$"*"$表示。

图中的直线,为护城河的最优挖掘方案,即能围住所有泉水的最短路线。

路线从左上角起,经过泉水的坐标依次是:$(18,0)$,$(6,-6)$,$(0,-5)$,$(-3,-3)$,$(-17,0)$,$(-7,7)$,$(0,4)$,$(3,3)$。绕行一周的路径总长为$70.8700576850888...$。答案只需要保留两位小数,于是输出是$70.87$。

Input

第$1$行:一个整数$N$。

第$2..N+1$行:每行包含$2$个用空格隔开的整数,$x[i],y[i]$,即第$i$股泉水的位置坐标。

Output

一行一个数字,表示满足条件的护城河的最短长度。保留两位小数。

Sample Input

20
2 10
3 7
22 15
12 11
20 3
28 9
1 12
9 3
14 14
25 6
8 1
25 1
28 4
24 12
4 15
13 5
26 5
21 11
24 4
1 8

Sample Output

70.87

HINT

$8\;\leq\;N\;\leq\;5000$.

Solution

求凸包周长.

#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 5005
#define eps 1e-11
using namespace std;
struct point{
int x,y;double t;
}a[N],v[N];
int n,u,vn;
inline double sqr(int k){
return (double)(k*k);
}
inline point dec(point x,point y){
return (point){x.x-y.x,x.y-y.y,0.0};
}
inline int mult(point x,point y){
return x.x*y.y-y.x*x.y;
}
inline double dis(point x,point y){
return sqrt(sqr(abs(x.x-y.x))+sqr(abs(x.y-y.y)));
}
inline bool cmp(point x,point y){
if(fabs(x.t-y.t)<eps)
return dis(x,a[1])>dis(y,a[1]);
return x.t<y.t;
}
inline void convex(){
u=1;
for(int i=2;i<=n;i++)
if((a[i].x<a[u].x)||(a[i].x==a[u].x&&a[i].y<a[u].y)) u=i;
a[0]=a[u];a[u]=a[1];a[1]=a[0];
for(int i=2;i<=n;i++)
a[i].t=atan2(a[i].y-a[1].y,a[i].x-a[1].x);
sort(a+2,a+1+n,cmp);
v[++vn]=a[1];v[++vn]=a[2];a[++n]=a[1];
for(int i=3;i<=n;i++){
if(fabs(a[i].t-a[i-1].t)<eps)
continue;
while(vn>1&&mult(dec(a[i],v[vn-1]),dec(v[vn],v[vn-1]))>0) vn--;
v[++vn]=a[i];
}
}
inline double cir(){
double ret=0;
for(int i=2;i<=vn;i++)
ret+=dis(v[i],v[i-1]);
return ret;
}
inline void init(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
convex();
printf("%.2lf\n",cir());
}
int main(){
freopen("convex.in","r",stdin);
freopen("convex.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return 0;
}

[bzoj1670][Usaco2006 Oct]Building the Moat的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...

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

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

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

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

随机推荐

  1. CSS常用渐变

    边框渐变: border-image: -webkit-linear-gradient( red , blue) 30 30; border-image: -moz-linear-gradient( ...

  2. JS高程5.引用类型(2)Array类型

    Array类型: ECMAScript数组的每一项可以保存任何类型的数据,数组的大小是可以动态调整的. 创建数组的基本方式: (1)使用Array构造函数 var color=new Array(); ...

  3. eCharts 数据转换json

    public ActionResult ShowChart() { return View(); } <div id="main" style="width:600 ...

  4. iOS 小谈开发者中的个人、组织(公司、企业)账号

    苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...

  5. Java 中的集合接口——List、Set、Map

    Java 中的集合接口——List.Set.Map 什么叫集合:集合就是Java API所提供的一系列类的实例,可以用于动态存放多个对象.这跟我们学过的数组差不多,那为什么我们还要学集合,我们看看数组 ...

  6. Hibernate 系列 08 - 对象识别机制

    目录导读: Hibernate 系列 学习笔记 目录 本篇目录: 为了区别不同的对象,有两种识别方法: 1. 内存地址识别(“==”号识别) 2. equals()和hashCode()识别 1. 以 ...

  7. Java 条形码 二维码 的生成与解析

    Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...

  8. struts2中各个jar包作用

    Struts2.3.4 所需的Jar包及介绍 Jar包的分类 jar包名称 jar包版本 jar包 文件名 jar包 的作用 jar包内包含的主要包路径及主要类 依赖的自有jar包名称 依赖的第三方j ...

  9. iOS网络编程

    今天的重点是UIWebView.NSURLSession.JSon. 网络编程联网准备:1.在Info.plist中添加AppTransportSecurity类型Dictionary:2.在AppT ...

  10. font-size 兼容问题

    早年~ 楔子 在为“我的抵扣券”添加  按钮时,为了将文字隐掉,给节点设置了“font-size:0;”,设置后刷一下浏览器,webkit下按钮掉下去了,而其他浏览器(包括IE6/7)都正常: 按理说 ...