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. 【BZOJ2243】[SDOI2011] 染色(树链剖分)

    点此看题面 大致题意: 有一棵\(n\)个节点的无根树和\(m\)个操作,且每个节点有一个颜色.操作有两种:一种是将两点树上路径之间所有点染成颜色\(c\),另一种是询问两点树上路径之间颜色段的数量. ...

  2. 【Python】bytes和hex字符串之间的相互转换。

    反复在几个环境上折腾码流的拼装解析和可读化打印,总是遇到hex字符串和bytes之间的转换,记录在这里吧. 1. 在Python2.7.x上(更老的环境真心折腾不起),hex字符串和bytes之间的转 ...

  3. 访问虚拟机中web服务的

    经常发现假如我们想弄一点小玩意或跑一些小demo,总是要不断的在自己的工作本本上搭建不同的运行环境,久而久之,本本上充斥着各种软件,速度下降了,同时管理也非常的不方便.于是想到用虚拟机来搭建运行环境, ...

  4. linux 环境能变量配置

    1, 3.配置环境变量 在/etc/profile文件末尾中添加以下环境变量:(我上面的JDK目录是jdk1.6.0_45,所以下面JAVA_HOME中也是这个) export JAVA_HOME=/ ...

  5. c++中 endl的意思?

    endl是 end line的意思,表示此行结束,换行,就是回车

  6. Java中json前后端日期传递处理

    这里推荐2种方式 依赖包 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifa ...

  7. pycharm clion rider 注册

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

  8. build path导入的jar失效导致找不到类

    今天碰到一个很奇葩的问题,搞起我以后都不敢 build path到jar了 所以我就全部放到lib目录下了,因为之前使用build path导入的jar失效了,一直找不类,具体原因我也不清楚,我之前的 ...

  9. mysql中的FROM_UNIXTIME()函数和UNIX_TIMESTAMP()函数

    unix_timestamp 是时间戳,可以用数据库里的存储时间数据的字段 from_unixtime 是将时间戳格式化为你想要时间

  10. python常用内置算法用到的单词音频

    http://boscdn.bpc.baidu.com/v1/developer/990a728b-ca96-4bd9-9124-5357d829bf70.mp3 百度广播开发平台生成