博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1392 Surround the Trees
阅读量:6090 次
发布时间:2019-06-20

本文共 2850 字,大约阅读时间需要 9 分钟。

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 193 Accepted Submission(s): 97
 
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

 

 

经典的几何凸包 。

有一个注意的地方是两个点的时候不用乘以2 。  

 

 

 

#include 
#include
#include
#include
#include
using namespace std;typedef long long LL;const double eps = 1e-8;const int N = 1100;struct node{ double x , y ; node(){}; node operator - (const node &a) const{ node res ; res.x = x - a.x ,res.y = y - a.y; return res; }}p[N],ch[N];int n ;inline bool cmp(node a , node b ){ if(a.x != b.x )return a.x < b.x ; else return a.y < b.y; }inline double Cross( node a , node b ){ return a.x * b.y - a.y *b.x ; }inline double dis( node a , node b ){ return sqrt( ( a.x- b.x ) * ( a.x- b.x ) + ( a.y- b.y ) * ( a.y- b.y ) ); }int ConvexHull(){ sort( p , p + n ,cmp ); int m = 0 ; for( int i = 0 ;i < n ; ++i ){ while( m > 1 && Cross( ch[ m-1 ] - ch[m-2] , p[i] - ch[m-2] ) <= 0 ) m-- ; ch[m++] = p[i]; } int k = m ; for( int i = n - 2 ; i >= 0 ; i -- ){ while ( ( m > k ) && Cross( ch[m-1] - ch[m-2] , p[i] - ch[m-2] ) <= 0 ) m--; ch[m++] = p[i]; } if( n > 1 ) m--; return m;}int main(){ double sum ; #ifdef LOCAL freopen("in","r",stdin); #endif while(~scanf("%d",&n)&&n) { for( int i = 0; i < n ;++i ){ scanf("%lf%lf", &p[i].x, &p[i].y); } if( n == 1 ){ puts("0.00"); continue; } if( n == 2 ) { printf("%.2lf\n",dis(p[0],p[1])); continue ;} int m = ConvexHull(); double ans = 0 ; for( int i = 0 ; i < m ; ++i ){ ans += dis( ch[i] ,ch[ (i+1) %m ] ); } printf("%.2lf\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/hlmark/p/3988440.html

你可能感兴趣的文章
调试json
查看>>
C - Surprising Strings
查看>>
hibernate里的generator中class =value介绍
查看>>
activity-alias的使用
查看>>
免费的天气预报API--谷歌,雅虎,中央气象台
查看>>
第36周日
查看>>
SQL Server 无法打开物理文件的 2 种解决办法
查看>>
推荐一款好用的文件/文件夹对比工具 —— Beyond Compare
查看>>
java设计模式--结构型模式--桥接模式
查看>>
JS window.open()属性
查看>>
JVM:从实际案例聊聊Java应用的GC优化
查看>>
关于Git的暂存区这个概念的理解.
查看>>
/dev/shm和swap差别与联系
查看>>
[翻译svg教程]svg中矩形元素 rect
查看>>
【百度地图API】如何给自定义覆盖物添加事件
查看>>
《大公司病》阅读笔记
查看>>
手机管理中的应用【6】——电源管理篇
查看>>
【Android工具】DES终结者加密时报——AES加密演算法
查看>>
效果收集-点击显示大图
查看>>
Android 开机过程PMS分析
查看>>