Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10299    Accepted Submission(s): 3991

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
 

Source

 
Graham扫描法求凸包,凸包周长即为答案。
 //2016.10.2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 105
#define eps 1e-8 using namespace std; struct point
{
double x, y;
point(){}
point(double a, double b):x(a), y(b){}
point operator-(point a){//向量减法
return point(x-a.x, y-a.y);
}
point operator+(point a){//向量加法
return point(x+a.x, y+a.y);
}
double operator*(point a){//向量叉积
return x*a.y-y*a.x;
}
bool operator<(const point a)const{
if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
return x<a.x;
}
double len(){//向量的模
return sqrt(x*x+y*y);
}
}p[N], s[N];//p为点,s为栈 double cp(point a, point b, point o)//向量oa,ob叉积
{
return (a-o)*(b-o);
} void Convex(point *p, int &n)//Graham扫描法,栈内为所有凸包点
{
sort(p, p+n);
int top, m;
s[] = p[]; s[] = p[]; top = ;
for(int i = ; i < n; i++)//从前往后扫
{
while(top> && cp(p[i], s[top], s[top-])>=)top--;
s[++top] = p[i];
}
m = top;
s[++top] = p[n-];
for(int i = n-; i >= ; i--)//从后往前扫
{
while(top>m && cp(p[i], s[top], s[top-])>=)top--;
s[++top] = p[i];
}
n = top;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF && n)
{
for(int i = ; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
sort(p, p+n);
int cnt = ;
for(int i = ; i < n; i++)//去掉重复的点
if(fabs(p[i].x-p[cnt].x)>eps || fabs(p[i].y-p[cnt].y)>eps)
p[++cnt] = p[i];
cnt++;
if(cnt == ){
printf("0.00\n");continue;
}else if(cnt==){
printf("%.2lf\n", (p[]-p[]).len());continue;
}
Convex(p, cnt);
double ans = ;
s[cnt] = s[];
for(int i = ; i < cnt; i++)ans+=(s[i+]-s[i]).len();
printf("%.2lf\n", ans);
} return ;
}
 

HDU1392(凸包)的更多相关文章

  1. hdu1392凸包裸题

    //极角排序 #include <bits/stdc++.h> #define sqr(x) ((x)*(x)) using namespace std; ],top; struct PO ...

  2. hdu1392 Surround the Trees 凸包

    第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行. 我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点. 然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点. #incl ...

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

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

  4. HDU1392:Surround the Trees(凸包问题)

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

  5. codevs1298, hdu1392 (凸包模板)

    题意: 求凸包周长. 总结: 测试模板. 代码: #include <iostream> #include <cstdio> #include <cstring> ...

  6. 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees

    [科普]什么是BestCoder?如何参加? Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  7. 凸包算法(Graham扫描法)详解

    先说下基础知识,不然不好理解后面的东西 两向量的X乘p1(x1,y1),p2(x2,y2) p1Xp2如果小于零则说明  p1在p2的逆时针方向 如果大于零则说明 p1在p2的顺时针方向 struct ...

  8. NYOJ-78 圈水池,凸包裸模板!

    圈水池 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 刚做完HDU1392,就看到这个题,嗯,原代码改改就过了. 题意不多说了,会凸包的话很简单,不会也不难,这道题时限是4s ...

  9. [poj1113][Wall] (水平序+graham算法 求凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

随机推荐

  1. javascript 数组合并

    javascript 中两个数组合并,当然可以遍历其中一个数组,通过push()方法将元素插入到另一个数组中. 另外,也可以使用内置的方法(javascript Array 对象上就具有的方法,或许比 ...

  2. CodeForces 609B The Best Gift

    统计+枚举 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...

  3. Quick Cocos2dx 版本更新

    呵呵,不出所料,我又把项目的quick x版本从2.2.1升级到2.2.3了,不知道下次升级到3.x回事神马时候呢,好期待的说. 话说运行2.2.3的player的时候,老是提醒我显卡不支持openg ...

  4. SecureCRT 密钥生成 SSH 使用密钥登陆 服务器

    1.首先“Tool”-“Create Public Key” 2. 选择RSA 模式 4. 创建短语密码 5. 密钥长度为1024 6. 选择标准的 key  , 下面是生成的路径 二 . 配置 SS ...

  5. (简单) POJ 1562 Oil Deposits,BFS。

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  6. php中使用mysql_fetch_object向页面输出结果,总结!

    public function selectResultByThird() { if ((!empty ($_REQUEST["bigname"])) && (!e ...

  7. mysql的 charset、collation、prefix了解

    charset,即字符集. collation,用于指定数据集如何排序,以及字符串的比对规则,即排序规则. prefix,即数据库里表使用的前缀. /************************* ...

  8. BMP085气压传感器驱动

    BMP085是新一代的小封装气压传感器,主要用于气压温度检测,在四轴飞行器上可以用作定高检测,该传感器属于IIC总线接口,依然沿用标准IIC驱动程序 使用该传感器需要注意的是我们不能直接读出转换好的二 ...

  9. Android NDK开发及调用标准linux动态库.so文件

    源:Android NDK开发及调用标准linux动态库.so文件 预备知识及环境搭建 1.NDK(native development Kit)原生开发工具包,用来快速开发C.C++动态库,并能自动 ...

  10. STM32的时钟树深入详解以及RCC配置

    在STM32上如果不使用外部晶振,OSC_IN和OSC_OUT的接法 如果使用内部RC振荡器而不使用外部晶振,请按照下面方法处理: 1)对于100脚或144脚的产品,OSC_IN应接地,OSC_OUT ...