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的 replace() 方法的使用讲解

    String.prototype.replace() The replace() method returns a new string with some or all matches of a p ...

  2. 外网主机访问虚拟机下的Web服务器_服务器应用_Linux公社-Linux系统门户网站

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  3. java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?

    java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种 ...

  4. GoEasy消息推送

    1. 从GoEasy获取appkey appkey是验证用户的有效性的唯一标识. 注册账号. GoEasy官网:https://goeasy.io 用注册好的账号登录到GoEasy的后台管理系统,创建 ...

  5. ubuntu如何安装Mac主题

    1.安装 Gnome 经典桌面 sudo apt-get install gnome-session-fallback 没有安装桌面的可安装 Gnome 桌面: sudo apt-get instal ...

  6. tableview的reloadData应注意

    http://blog.csdn.net/ouyangtianhan/article/details/7835041 http://stackoverflow.com/questions/160715 ...

  7. Java XML解析器

    使用Apache Xerces解析XML文档 一.技术概述 在用Java解析XML时候,一般都使用现成XML解析器来完成,自己编码解析是一件很棘手的问题,对程序员要求很高,一般也没有专业厂商或者开源组 ...

  8. excel导入到Orcle

    Excel导入到Oracle中 在Oracle中创建一个表,与excel的表头对应 将excel文件保存为.csv格式 创建一个.ctl文件 load data infile 'd:\xiaoyou. ...

  9. Java中实现Serializable接口为什么要声明serialVersionUID?

    什么情况下需要修改serialVersionUID 的值?      序列化运行时使用一个称为 serialVersionUID 的版本号与每个可序列化类相关联,该序列号在反序列化过程中用于验证序列化 ...

  10. oracle关键字(保留字)

    其实这个东西可以在oracle 上输入一个sql语句就可以得到: select * from v$reserved_words order by keyword asc;   //order 后边不是 ...