bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
Time Limit: 3 Sec Memory Limit: 64 MB
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
HINT
Source
凸包:
(1)找出点集p[]中最左下的点p1,把p1同点集中其他各点用线段连接,并计算这些线段的斜率,然后按斜率从小到大排序,得到新的节点序列p1,p2,...pn.依次连接这些点,得到一个多边形(已经逆时针,有所进展,但还需去掉不在凸包上的点)。此时p1是凸包的边界起点,p2和pn也是最终凸包的顶点,p[n+1]=p1(看成循环的)
(2)删除p3,p4,...p[n-1]中不在凸包上的点:
先把p1,p2,p3入栈S中,再依次循环(i = 3 -> n-1),若栈顶的两个点和当前的点p[i]这三点连线的方向向顺时针方向偏转,表明是凹的,应删除,则栈顶元素出栈(要循环判断,即可能前面的仍是凹的,还需再出栈,举例如下图),直到向逆时针方向偏转或者栈内只有2个元素了(p1p2),就把当前点p[i]入栈。
最后栈中的元素就是最终凸包上的点。
----------------
---------------
--------------------
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define db double
#define N 5050
#define inf 100000009
char xB[<<],*xS=xB,*xTT=xB;
#define getc() (xS==xTT&&(xTT=(xS=xB)+fread(xB,1,1<<15,stdin),xS==xTT)?0:*xS++)
#define isd(c) (c>='0'&&c<='9')
inline int read(){
char xchh;
int xaa;
while(xchh=getc(),!isd(xchh));(xaa=xchh-'');
while(xchh=getc(),isd(xchh))xaa=xaa*+xchh-'';return xaa;
}
struct tb{db x,y,k;}p[N],s[N];
bool cmp(tb a,tb b){return a.k<b.k;}
db mul(tb a,tb b,tb c){return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);}
db dis(tb a,tb b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
int n,x,y,tp=;
db ans;
int main()
{
n=read();
for(int i=;i<n;i++)
{
x=read();y=read();
p[i]=(tb){(db)x,(db)y};
if(x<p[].x||(x==p[].x&&y<p[].y)) swap(p[],p[i]);
}
for(int i=;i<n;i++) p[i].k=(p[i].x==p[].x?inf:(p[i].y-p[].y)/(p[i].x-p[].x));
sort(p+,p+n,cmp);
p[n]=p[];s[]=p[];s[]=p[];
for(int i=;i<=n;i++)
{
while(tp&&mul(p[i],s[tp],s[tp-])>=) tp--;
s[++tp]=p[i];
}
for(int i=;i<tp;i++) ans+=dis(s[i],s[i+]);
printf("%.2lf\n",ans);
return ;
}
bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包的更多相关文章
- bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...
- BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
Description 求凸包周长. Sol 凸包+计算几何. 这好像叫什么 Graham Scan 算法... 这个可以求凸包的周长,直径,面积. 选择一个基点,然后按极角排序,最后用一个栈一直维护 ...
- 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)
链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】
凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...
- 【BZOJ】1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)
http://www.lydsy.com/JudgeOnline/problem.php?id=1670 裸打了凸包.. #include <cstdio> #include <cs ...
- BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包
BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...
- 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 ...
- bzoj 1670 Building the Moat护城河的挖掘 —— 凸包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 单调栈维护凸包即可,用叉积判断: 维护上凸壳,然后把所有点的纵坐标翻转再求上凸壳即可, ...
随机推荐
- python初步学习-面向对象之 类(二)
方法重写 如果你的父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法: #!/usr/bin/env python #coding:utf8 class Parent: def myMeth ...
- Spring Data JPA笔记
1. Spring Data JPA是什么 Spring Data JPA是Spring Data大家族中的一员,它对对持久层做了简化,用户只需要声明方法的接口,不需要实现该接口,Spring Dat ...
- Go语言 2 变量、常量和数据类型
文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 2.1 变量 变量是对一块内存空间的命名,程序可以通 ...
- 打表找规律C - Insertion Sort Gym - 101955C
题目链接:https://cn.vjudge.net/contest/273377#problem/C 给你 n,m,k. 这个题的意思是给你n个数,在对前m项的基础上排序的情况下,问你满足递增子序列 ...
- Sqlmap与burpsuite动态更新某些参数
有如下注入点: http://localhost/id=1&order_nu=1 情况说明: id为注入点, 每一次注入时, order_nu不能跟上次的一样(假说这个order_nu为一个 ...
- 用C#实现对MSSqlServer数据库的增删改查---Server层(WaterLevelRecordServer.cs、DeviceRecordServer.cs)
抛开现实的残酷与无奈,对技术孜孜不倦的追求,从专注到执着,从疯狂到忘我,始终坚信代码可以改变世界,更能改变自己的人生. WaterLevelRecordServer.cs using System; ...
- mysqldump 逻辑备份的正确方法【转】
1. 利用mysqldump进行逻辑备份 1)全逻辑备份: mysqldump -uxxx -p --flush-logs --delete-master-logs --all-databases & ...
- 谁说运维用ELK没用?我就说很有用,只是你之前不会用【转】
1.安装JDK 1)登陆ORACLE官网 (http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html ...
- html的loadrunner脚本
Action(){ char strs[20]; lr_start_transaction("api_sync_order"); web_add_header("SO ...
- 《深入理解Java虚拟机》笔记--第四章、虚拟机性能监控与故障处理工具
主要学习并记录在命令行中操作服务器时使用的六大命令工具,可视化工具JConsole和VisualVM在开发过程中熟悉. 一.jps:虚拟机进程状况工具(JVM Process Status Tool) ...