Surround the Trees(凸包)
来源:互联网 发布:发票数据导出excel 编辑:程序博客网 时间:2024/05/17 13:44
A - Surround the Trees
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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.
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.
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
#include<iostream>#include<vector>#include<cmath>#include<algorithm>#include<cstdio>using namespace std;#define esp 1e-6#define PI acos(-1)int Sign(double x){//判断x大于0,小于0,还是等于0 return fabs(x)<esp?0:x>0?1:-1;}struct Point{ //存点 double x,y;Point(){}Point(double xx,double yy):x(xx),y(yy) { }Point operator-(const Point & p) const {return Point(x-p.x,y-p.y);}bool operator <(const Point & p) const {if( y < p.y)return true;else if( y > p.y)return false;elsereturn x < p.x;}};typedef Point Vector;double Cross(const Vector & v1, const Vector & v2){//叉积return v1.x * v2.y - v2.x * v1.y;}double Distance(const Point & p1,const Point & p2){//求距离 return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y-p2.y)*(p1.y-p2.y));}struct Comp { //用来定义极角排序规则的函数对象Point p0; //以p0为原点进行极角排序,极角相同的,离p0近算小Comp(const Point & p):p0(p.x,p.y) { }bool operator ()(const Point & p1,const Point & p2) const {int s = Sign( Cross(p1-p0,p2-p0));if( s > 0)return true;else if( s < 0)return false;else {if( Distance(p0,p1)<Distance(p0,p2))return true;elsereturn false;}}};int Graham(vector<Point> & points,vector<Point> & stack) {//求凸包 //points是点集合if( points.size() < 3)return 0; //返回凸包顶点数stack.clear();//先按坐标排序,最左下的放到points[0]sort(points.begin(),points.end());//以points[0] 为原点进行极角排序sort(points.begin()+1,points.end(),Comp(points[0]));stack.push_back(points[0]);stack.push_back(points[1]);stack.push_back(points[2]);for(int i = 3; i< points.size(); ++i) {while(true) {Point p2 =* (stack.end()-1);Point p1 = *(stack.end()-2);if( Sign(Cross(p2-p1,points[i]-p2) <= 0))//p2->points[i]没有向左转,就让p2出栈stack.pop_back();elsebreak;}stack.push_back(points[i]);}return stack.size();}int main(){ double N,L,l,n1; int n; Point p; vector<Point> points;vector<Point> stack; while(~scanf("%d",&n)&&n!=0){ double res=0; points.clear(); stack.clear(); for(int i=0;i<n;i++){ scanf("%lf %lf",&n1,&l); p.x=n1,p.y=l; points.push_back(p);}int size = Graham(points,stack);//顶点个数if(size>=1){for(int i=0;i<size-1;i++)//求凸包周长 res+=Distance(stack[i],stack[i+1]); res+=Distance(stack[0],stack[size-1]); res+=2*PI*L; printf("%.2lf\n",res);}else{if(points.size()==1)//只有一个点printf("0.00\n");else if(points.size()==2)//只有两个点{res=Distance(points[0],points[1]);printf("%.2lf\n",res); } } } return 0;}
阅读全文
0 0
- Surround the Trees(凸包)
- Surround the Trees(凸包)
- ZJU 1453(hdu 1392) Surround the Trees(凸包)
- hdu1392(Surround the Trees)凸包问题
- HDU1392:Surround the Trees(凸包问题)
- hdu 1392 Surround the Trees(凸包)
- HDU-#1392 Surround the Trees(Graham & Andrew +凸包)
- hdu 1392 Surround the Trees( 凸包问题)
- hdoj Surround the Trees 1392 (凸包)
- HDU 1392 Surround the Trees(凸包模板)
- HDU1392 Surround the Trees(凸包模版题)
- HDU 1392 Surround the Trees(凸包)
- HDU 1392 Surround the Trees(凸包)
- Surround the Trees&&凸包入门题
- HDU1392--Surround the Trees--凸包
- HDU 1392 Surround the Trees(凸包)
- hdu_1392 Surround the Trees(凸包)
- HDUJ 1392 Surround the Trees 凸包
- Cron表达式
- 前后端表单校验实例
- PHP day9 TP post接受数据 variable type error:array
- 线段树题目整合
- solr Field、CopyField、DynamicField
- Surround the Trees(凸包)
- Base64的三种实现方式
- SSL renegotiation攻击
- 前端内容命名
- Qsort 快排
- 数据结构之头结点链表的三种插入方式(头插法,尾插法,在pos处插入)
- 栈的压入、弹出序列
- javascript
- 机器学习-->特征降维方法总结