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

Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him? 
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.

 
Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.

 
Output
The minimal length of the rope. The precision should be 10^-2.

 
Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
 
0
 
Sample Output
243.06

题目链接: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(凸包入门)的更多相关文章

  1. 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 ...

  2. HDU - 1392 Surround the Trees (凸包)

    Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...

  3. hdu 1392 Surround the Trees (凸包)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. hdu 1392 Surround the Trees 凸包模板

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1392 Surround the Trees 凸包裸题

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 1392 Surround the Trees(凸包*计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...

  7. hdu 1392:Surround the Trees(计算几何,求凸包周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU-1392 Surround the Trees,凸包入门!

    Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...

  9. HDUJ 1392 Surround the Trees 凸包

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 国外常用代理IP对比【仅供参考】

    国外常用代理IP对比[仅供参考]http://www.it588.cn/vmware/2019-03-22/547.html

  2. 2018.6.13 Java语言基础复习总结

    Java语言基础与面向对象编程实践 第一章 初识Java 1.1机器语言 机器语言是指一台计算机全部的指令集合.机器语言室友0和1组成的二进制数,是一串串由0和1组成的指令序列,可将这些指令序列交给计 ...

  3. Java反射得到属性的值和设置属性的值

    package com.whbs.bean; public class UserBean { private Integer id; private int age; private String n ...

  4. 10个HTML5 实战教程 提升你的综合开发能力

    HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...

  5. java字符格式

    http://blog.chinaunix.net/uid-12348673-id-3335300.html http://blog.csdn.net/zhouyong80/article/detai ...

  6. java调用摄像头

    http://blog.csdn.net/xing_sky/article/details/43482213 原文地址:http://blog.csdn.net/zajin/article/detai ...

  7. 后台调用前台js

    WEB后台代码调用前台JS(两种方式). 1   这种方式只能调用简单的JS代码.不能调用自定义的函数. string jss = "<script language='javascr ...

  8. 使用Xcode过程中遇到的问题

    前言:记录一下使用Xcode过程中遇到的问题 1.关于开发者的Team的问题,是选用自己的个人Team还是选用公司的付费的Team(本机环境:Xcode9 + iPad :iOS11.0.3) 问题: ...

  9. pycharm clion rider 注册

    JetBrains 公司出品的pycharm clion rider 专业版本都需要注册才能运行,这里有个免费注册方法: JetBrains授权服务器2017.10.7授权方法:激活时选择Licens ...

  10. php扩展开发-哈希表

    什么是哈希表呢?哈希表在数据结构中也叫散列表.是根据键名经过hash函数计算后,映射到表中的一个位置,来直接访问记录,加快了访问速度.在理想情况下,哈希表的操作时间复杂度为O(1).数据项可以在一个与 ...