BZOJ_1670_[Usaco2006 Oct]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

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

求凸包周长。
atan2函数效率不高,这里按照斜率排序。
然后单调栈求一遍凸包即可。
 
代码:
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
typedef double f2;
#define N 5050
struct Point {
f2 x,y;
Point() {}
Point(f2 x_,f2 y_) :
x(x_),y(y_) {}
Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);}
Point operator + (const Point &p) const {return Point(x+p.x,y+p.y);}
Point operator * (f2 rate) const {return Point(x*rate,y*rate);}
}a[N];
f2 dot(const Point &p1,const Point &p2) {return p1.x*p2.x+p1.y*p2.y;}
f2 cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
f2 dis(const Point &p1,const Point &p2) {return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}
int n,o,S[N],top;
bool cmp(const Point &p1,const Point &p2) {
f2 tmp=cross(p1-a[1],p2-a[1]);
if(abs(tmp)<1e-6) return dis(p1,a[1])<dis(p2,a[1]);
else return tmp>0;
}
int main() {
scanf("%d",&n);
int i;
for(i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
o=1;
for(i=2;i<=n;i++) if(a[i].x<a[o].x||(a[i].x==a[o].x&&a[i].y<a[o].y)) o=i;
swap(a[1],a[o]);
sort(a+2,a+n+1,cmp);
for(i=1;i<=n;i++) {
while(top>1&&cross(a[i]-a[S[top-1]],a[S[top]]-a[S[top-1]])>0) top--;
S[++top]=i;
}
f2 ans=0;
for(i=1;i<top;i++) {
ans+=dis(a[S[i]],a[S[i+1]]);
}
printf("%.2f\n",ans+dis(a[1],a[S[top]]));
}

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护城河的挖掘(凸包)

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

  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护城河的挖掘

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【转】关于大型网站技术演进的思考(二十)--网站静态化处理—web前端优化—中(12)

    Web前端很多优化原则都是从如何提升网络通讯效率的角度提出的,但是这些原则使用的时候还是有很多陷阱在里面,如果我们不能深入理解这些优化原则背后所隐藏的技术原理,很有可能掉进这些陷阱里,最终没有达到最佳 ...

  2. poj 2337 && zoj 1919 欧拉回路+连通性判断

    题目要求按字典序排列,而且可能有重边 所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面 这里有一点问题就是我一开始使用的是qsort: int cmp(con ...

  3. hibernate保存oracle日期时间

    用java生成一个带年月日时分秒的时间,通过hibernate对象保存到oracle中的Date字段中, 第一种方法: java实体类的createDate属性,类型为java.util.Date h ...

  4. Eval 和 Bind 的区别

    原文发布时间为:2008-10-20 -- 来源于本人的百度文章 [由搬家工具导入] 据绑定表达式包含在 <%# 和 %> 分隔符之内,并使用 Eval 和 Bind 函数。 Eval 函 ...

  5. 内存管理——(exceptional C++ 条款9,条款10)

    C++的各个内存区域: (1)常量数据(const data)区 常量数据区存储的是字符串等在编译期间就能确定的值,在整个程序的生命周期内,这里的数据都是可用.区域内所有的数据都是 只读的. (2)栈 ...

  6. msp430项目编程07

    msp430中项目---简单计算器 1.扫描键盘工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  7. python学习之-- redis模块基本介绍

    数据缓存系统: 1:mongodb:是直接持久化,直接存储于硬盘的缓存系统 2:redis: 半持久化,存储于内存和硬盘 3:memcache:数据只能存储在内存里的缓存系统关于memcache 学习 ...

  8. HDU 5883 欧拉路径异或值最大 水题

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  9. C. Wilbur and Points---cf596C

    http://codeforces.com/problemset/problem/596/C 题目大意:  给你n个数对  确保如果(x,y)存在这个集合  那么  0<=x'<=x &a ...

  10. IDEA添加作者注释

    1.打开IDEA的settings,然后在Editor下找到File and Code Templates 2.然后选择File Header 选择需要注释的的格式即可.