bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]
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
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
/**/
#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护城河的挖掘 [凸包模板题]的更多相关文章
- 【BZOJ-1670】Building the Moat护城河的挖掘 Graham扫描法 + 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 464 Solv ...
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Description 为了防止 ...
- bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...
- bzoj 1670 Building the Moat护城河的挖掘 —— 凸包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 单调栈维护凸包即可,用叉积判断: 维护上凸壳,然后把所有点的纵坐标翻转再求上凸壳即可, ...
- bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 387 Sol ...
- BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包
BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...
- 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)
链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...
- 【计算几何】【凸包】bzoj1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
随机推荐
- JMeter学习(二)录制脚本
---------------------------------------------------------------------------------------------------- ...
- QTP 10 安装及破解
QTP(QuickTest Professional),是一款比较优秀的商业自动化测试工具,主要用于web项目和C/S结构程序的测试. QTP具有的一大特性:关键字驱动测试(keyword-drive ...
- NSDictionary(key与value)
1.key与value关系,用一个key的值控制整个模型 NSDictionary *dic = @{@"channelKey":channelModel,@"chann ...
- javascript设置网页刷新或者重新加载后滚动条的位置不变
有个同事说再javascript中你可以做任何你想做的事情,当时觉得不以为然,今天遇到个问题,就是页面重新加载后总是回到页面的顶部,如果客户只想看到他想看到的部分是怎么变化的,这个体验就好了.原本想象 ...
- 对window的认识
首先要明确: 不管是全局的函数还是全局的变量,都是属于window的,例如: a = 12; //全局变量 alert(a) === alert(window.a) function show(){ ...
- 层叠上下文(The stacking context)
MDNThe stacking context 层叠上下文是HTML元素的三维概念,这些HTML元素在一条假想的相对于面向(电脑屏幕的)视窗或者网页的用户的z轴上延伸,HTML元素依据其自身属性按照优 ...
- ruby 元编程
一 对象模型 kernel Module Kernel.private_instance_methods.grep(/^pr/) private method 1 如果一个方法接收者不是你自己,一 ...
- [服务]ftp主动模式和被动模式
经常忘记这个东西.于是总结下这东西感受下这个协议. FTP连接方式 控制连接:标准端口为21,用于发送FTP命令信息 数据连接:标准端口为20,用于上传.下载数据 数据连接的建立类型: 主动模式:服务 ...
- JS第一个动画
描述:让页面中的一个盒子平稳向右移动到指定位置 <div id="div1"> <div> JS代码实现 <script type="tex ...
- 获取iOS系统版本 --- UIDevice
UIDevice类是一个单例,其唯一的实例( [UIDevice currentDevice] ) 代表了当前使用的设备. 通过这个实例,可以获得设备的相关信息(包括系统名称,版本号,设备模式等等). ...