HDU 1392 Surround the Trees(凸包入门)
2024-08-29 22:04:47
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.
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;
}
最新文章
- Delphi_03_Delphi_Object_Pascal_基本语法_01
- UILabel多种字体
- ABAP--在查询条件只包含部分索引字段时,如何使用索引
- UIWindows&;nbsp;使用注意
- morhia解决BigDecimal映射问题
- Eclipse 调试 Java 程序的技巧
- 获取Application中的spring容器
- org.springframework.jdbc.datasource
- Python数据预处理—训练集和测试集数据划分
- doubango(2)--底层协议栈结构分析
- 华为手机无法使用USB调试的解决方案
- Java_异常处理
- 再见了Server对象,拥抱IHostingEnvironment服务对象(.net core)
- python note 15 正则表达式
- Latex常用
- SQL Server如何查看当前数据库连接的SPID
- iOS 模拟器“安装”app
- C++11并发——多线程std::thread (一)
- 2018-2019-2 网络对抗技术 20165320 Exp4 恶意代码分析
- jQuery中的ajax用法案例