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. js 实现纯前端将数据导出excel两种方式,亲测有效

    由于项目需要,需要在不调用后台接口的情况下,将json数据导出到excel表格,兼容chrome没问题,其他还没有测试过 通过将json遍历进行字符串拼接,将字符串输出到csv文件,输出的文件不会再是 ...

  2. css img 等比例自动缩放

    按父容器宽度自动缩放,并且保持图片原本的长宽比 img{ width: auto; height: auto; max-width: 100%; max-height: 100%; }

  3. 深入理解React虚拟DOM

    一.什么是虚拟DOM 虚拟DOM可以看做一棵模拟了DOM树的JavaScript对象树.比如: var element = { element: 'ul', props: { id:"uli ...

  4. Java第六次作业:RuPengGame setGameSize setGameTitle alert loadBgView playSound pause closeSound confirm input createText setTextPosition setTextColor setTextFontSize hideText showText CreateImage(number)

    package com.swift; import java.awt.Color; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 p ...

  5. IDEA注释模板设置

    1. 文件注释 1.1 Java类文件注释 Setting -> Editor -> File and Code Templates -> Includes -> File H ...

  6. cf540D. Bad Luck Island(概率dp)

    题意 岛上有三个物种:剪刀$s$.石头$r$.布$p$ 其中剪刀能干掉布,布能干掉石头,石头能干掉剪刀 每天会从这三个物种中发生一场战争(也就是说其中的一个会被干掉) 问最后仅有$s/r/p$物种生存 ...

  7. cf550D. Regular Bridge(构造)

    题意 给出一个$k$,构造一个无向图,使得每个点的度数为$k$,且存在一个桥 Sol 神仙题 一篇写的非常好的博客:http://www.cnblogs.com/mangoyang/p/9302269 ...

  8. linux文件属性之时间戳及文件名属性知识

    7  8  9 三列是时间(默认是修改时间) modify 修改时间  -mtime  修改文件内容 change 改变时间 -ctime  文件属性改变 access 访问时间  -atime  访 ...

  9. 与SVN相关的程序的调试问题【转】

    解决eclipse中出现Resource is out of sync with the file system问题. 分析:有时候因为时间紧迫的原因,所以就没去管它,今天再次遇到它,实在看着不爽,所 ...

  10. php中关于empty()函数是否为真的判断

    <?php// $a = 0;  ==> 符合empty,empty($a)为true// $a = '0';  ==> 符合empty,empty($a)为true// $a = ...