HDU 1392 Surround the Trees(凸包入门)
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10803 Accepted Submission(s): 4187
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.

There are no more than 100 trees.
Zero at line for number of trees terminates the input for your program.
题目链接:HDU 1392
基本没接触过计算几何,但感觉凸包还是得学习一个的,题意给你N个点,求用最短的绳子把这些点都围起来,显然就是凸包了首先选出一个点作为参考点:最左下角的点;然后得知道叉积这个东西:设三个点为a,b,c且b在a上方,c在b上方,对a->b向量和a->c向量作叉积,若得到的值大于零则说明这三个点从a数到c呈逆时针分布;若小于零,则呈顺时针分布;若等于0则说明共线。由于叉积在物理中用的比较多,正负的判别实际可以用右手定理和所成平面的方向之间关系可以判断,然后用Graham的扫描法方法就可以计算出凸包,当然这题若只有两个点答案就是两点中间的距离
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
struct Point
{
double x,y;
Point operator-(Point rhs)
{
rhs.x=x-rhs.x;
rhs.y=y-rhs.y;
return rhs;
}
}P[N],st[N]; double getlen(Point a,Point b)
{
a=a-b;
return sqrt(a.x*a.x+a.y*a.y);
}
double Cross(Point a,Point b,Point c)
{
Point ab=b-a,ac=c-a;
return (ab.x*ac.y)-(ab.y*ac.x);
}
bool cmp(Point a,Point b)
{
double x=Cross(P[0],a,b);
if(x>0)
return true;
if(x<0)
return false;
return getlen(P[0],a)<getlen(P[0],b);
}
int main(void)
{
int n,i;
while (~scanf("%d",&n)&&n)
{
for (i=0; i<n; ++i)
scanf("%lf%lf",&P[i].x,&P[i].y);
for (i=0; i<n; ++i)
if(P[i].y<P[0].y||(P[i].y==P[0].y&&P[i].x<P[0].x))
swap(P[i],P[0]);
sort(P+1,P+n,cmp);
int top=-1;
st[++top]=P[0];
st[++top]=P[1];
for (i=2; i<n; ++i)
{
while (top>0&&Cross(st[top-1],st[top],P[i])<=0)
--top;
st[++top]=P[i];
}
if(n==1)
puts("0");
else if(n==2)
printf("%.2f\n",getlen(P[0],P[1]));
else
{
double ans=0;
for (i=0; i<top; ++i)
ans+=getlen(st[i],st[i+1]);
ans+=getlen(st[top],st[0]);
printf("%.2f\n",ans);
}
}
return 0;
}
HDU 1392 Surround the Trees(凸包入门)的更多相关文章
- HDU 1392 Surround the Trees (凸包周长)
题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees 凸包模板
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees 凸包裸题
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- HDUJ 1392 Surround the Trees 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- Python实现注册和登录
一.注册账号需要实现的功能 1.输入:用户名,密码,密码确认 2.限制1:输入的账号和密码不能为空 3.限制2:两次输入密码必须一致 4.限制3:用户名不能重复 5.限制4:错误次数为4次 6.用字典 ...
- C的xml编程-libxml2
这里主要讲述libxml2在linux下的使用. (以下内容除了linux下的安装步骤是自己写的,其余均出自http://www.blogjava.net/wxb_nudt/archive/2007/ ...
- vue axios 攻略
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特点: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 http 请求 支持 ...
- jQuery支持链式编程,一句话实现左侧table页+常用筛选器总结
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Apache 配置默认编码
找到apache配置文件 httpd.conf ,找到以下内容 # # Specify a default charset for all content served; this enables # ...
- 2- vue django restful framework 打造生鲜超市 -环境搭建
使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...
- 自动化运维工具——ansible系列命令
ansible-galaxy 连接 https://galaxy.ansible.com 下载相应的roles,此网站是Ansible爱好者将日常使用较好的playbooks打包上传,其他人可以免费下 ...
- Python知识点入门笔记——特色数据类型(列表)
Python中提供了列表这种数据类型(类型为list)来存储多个值构成的序列 用逗号将不同数据分隔开,整体放在一个方括号[]里就创建了列表 列表中的数据类型可以是相同的,也可以是不同的 列表中还可以嵌 ...
- MTCNN自己的学习理解
MTCNN 流程 经过三个网络 P-Net,R-Net,O-Net 对于P-Net: P-Net是一个全卷积层,不涉及到全连接层,所以我们的输入图像的尺寸可以是不固定的. 对于P-Net来说,我们的输 ...
- pandas知识点(处理缺失数据)
pandas使用浮点值NaN表示浮点和非浮点数组中的缺失数据: In [14]: string_data = Series(['aardvark','artichoke',np.nan,'avocad ...