Description

为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河。农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两 股泉水。护城河必须能保护所有的泉水,也就是说,能包围所有的泉水。泉水一定在护城河的内部,或者恰好在河道上。当然,护城河构成一个封闭的环。 挖护城河是一项昂贵的工程,于是,节约的FJ希望护城河的总长度尽量小。请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少。 所有泉水的坐标都在范围为(1..10,000,000,1..10,000,000)的整点上,一股泉水对应着一个唯一确定的坐标。并且,任意三股泉水 都不在一条直线上。 以下是一幅包含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

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

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
 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int mxn=;
int n;
struct P{
int x,y;
}p[mxn],s[mxn];
int top=;
double sum=; inline int read(){
int x=,f=;
char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();};
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();};
return x*f;
}
inline P operator -(P a,P b){
P t; t.x=a.x-b.x; t.y=a.y-b.y; return t;
}
inline ll operator *(P a,P b){
return a.x*b.y-a.y*b.x;
}
inline ll dis(P a,P b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline bool operator < (P a,P b){
ll t=(a-p[])*(b-p[]);
if(t==)return dis(p[],a)<dis(p[],b);
return t>;
} void graham(){
int t=,i;
for(int i=;i<=n;i++)
if(p[i].y<p[t].y || (p[i].y==p[t].y&&p[i].x<p[t].x))t=i;//找出y值最小点作为起点
swap(p[],p[t]);
sort(p+,p+n+);
s[++top]=p[];
s[++top]=p[];
for(i=;i<=n;i++){
while((s[top]-s[top-])*(p[i]-s[top-])<=) top--;//找到“更靠外的点”就舍弃栈顶的点
s[++top]=p[i];
}
s[top+]=p[];
for(i=;i<=top;i++){
sum+=sqrt(dis(s[i],s[i+]));//计算距离
}
return;
}
int main(){
n=read();
int i,j;
int x,y;
for(i=;i<=n;i++){
p[i].x=read();p[i].y=read();
}
graham();
printf("%.2lf\n",sum);
return ;
}

bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]的更多相关文章

  1. 【BZOJ-1670】Building the Moat护城河的挖掘 Graham扫描法 + 凸包

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 464  Solv ...

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

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

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

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

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

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

  5. bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 387  Sol ...

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

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

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

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

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

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

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

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

随机推荐

  1. Linux下如何知道文件被那个进程写

    http://blog.yufeng.info/archives/2581#more-2581

  2. 视频会议的3G智能手机移植技术

    现今的视频会议系统已经兼容3G手机等移动终端设备,而3G智能手机使用的操作系统一般与PC的操作系统不一样,其开发环境一般都在PC上进行,通过模拟器在PC上进行手机系统的应用程序开发,而在这些操作系统上 ...

  3. mousewheel 模拟滚动

    div{ box-sizing:border-box; } .father{ width:500px; height:400px; margin:auto; margin-top: 50px; bor ...

  4. no.4 抽奖测试

    #-*-coding=gbk-*- import sys import random a=[] try: for x in range(1,20+1,1): #打印20人数编号 a.append(x) ...

  5. 高性能网站性能优化与系统架构(ZT)

    转载请保留出处:俊麟 Michael’s blog (http://space.itpub.net/7311285/viewspace-97) 我在CERNET做过拨号接入平台的搭建,而后在Yahoo ...

  6. .Net中的异步编程总结

    一直以来很想梳理下我在开发过程中使用异步编程的心得和体会,但是由于我是APM异步编程模式的死忠,当TAP模式和TPL模式出现的时候我并未真正的去接纳这两种模式,所以导致我一直没有花太多心思去整理这两部 ...

  7. CSS 动画之九-会呼吸的信封

    新年已经到来,各个网站都举办着各种不同类型的活动,'会呼吸的信封'有可能就是你遇到的其中一种.其实就是一个信封的样式,在封口处加上开合开合的动画效果,吸引用户去打开这个信封,点击后可能会送红包,优惠券 ...

  8. CSS 实现加载动画之二-圆环旋转

    上次简单的介绍了下如何用代码实现菊花旋转的加载动画,动画点击,这次继续我们的动画系列,实现另外一种加载动画,圆环旋转.与上次不同的是,菊花旋转是通过改变元素透明度来实现动画,这次因为考虑到元素叠加,加 ...

  9. Caffe学习系列(8):solver优化方法

    上文提到,到目前为止,caffe总共提供了六种优化方法: Stochastic Gradient Descent (type: "SGD"), AdaDelta (type: &q ...

  10. 学习Shell脚本编程(第4期)_在Shell程序中的使用变量

    变量的赋值 变量的访问 变量的输入 4.1 变量的赋值     在Shell编程中,所有的变量名都由字符串组成,并且不需要对变量进行声明.要赋值给一个变量,其格式如下: 变量名=值  注意: 等号(= ...