HDU ACM 1392 Surround the Trees->凸包
分析:直接求出凸包。再算边长就可以。
另外仅仅有一个点时为0.00单独处理,两个点直接为距离也单独处理。
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; struct Point
{
Point(){}
Point(double _x,double _y):x(_x),y(_y){}
Point operator-(const Point& a) const
{
return Point(x-a.x,y-a.y);
}
double x,y;
}; double dis(const Point& a,const Point& b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double cross(const Point& a,const Point& b)
{
return a.x*b.y-a.y*b.x;
} bool cmp(const Point& a,const Point& b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
} int convexhull(Point* p,int n,Point* ch)
{
int i,m,k; sort(p,p+n,cmp);
m=0;
for(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];
}
k=m;
for(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()
{
Point a[105],p[105];
int n,i,m;
double ans; while(scanf("%d",&n)==1 &&n)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
if(n==1)
printf("0.00\n");
else if(n==2)
{
printf("%.2lf\n",dis(a[0],a[1]));
}
else
{
m=convexhull(a,n,p);
ans=0;
for(i=1;i<=m;i++)
ans+=dis(p[i],p[i-1]);
printf("%.2lf\n",ans);
}
}
return 0;
}
HDU ACM 1392 Surround the Trees->凸包的更多相关文章
- HDU 1392 Surround the Trees(凸包入门)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题解报告:hdu 1392 Surround the Trees(凸包入门)
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
- HDU 1392 Surround the Trees(凸包)
题面 懒得粘贴了... 大致题意:坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 题解 直接求出凸包,统计一遍答案即可 #include<iostream> #include< ...
- HDU 1392 Surround the Trees(凸包)题解
题意:给一堆二维的点,问你最少用多少距离能把这些点都围起来 思路: 凸包: 我们先找到所有点中最左下角的点p1,这个点绝对在凸包上.接下来对剩余点按照相对p1的角度升序排序,角度一样按距离升序排序.因 ...
- HDU 1392 Surround the Trees 构造凸包
又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- hdu 1392 Surround the Trees 凸包模板
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- poi 操作excel
poi操作 创建一个excel关联对象HSSFWorkbook: HSSFWorkbook book = new HSSFWorkbook(); 创建一个sheet: HSSFSheet st = b ...
- [转贴]实践:C++平台迁移以及如何用C#做C++包装层
终于有个C++ 如何调用C#类库的文章,收藏之 在前面,我们看过OpenTK与MOgre,这二个项目都是C#项目,但是他的实现都是C++.他们简单来说就是一个包装层.常见的包装方式有二种,一 种就是我 ...
- delphi-json组件,速度非常快,要比superobject快好几倍
delphi-json组件,速度非常快,要比superobject快好几倍https://github.com/ahausladen/JsonDataObjectshttp://bbs.2ccc.co ...
- HTML图片热点、网页划区、拼接、表单
一.图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 二.网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 三.网页的拼接: ...
- Android开源项目发现---GridView 篇(持续更新)
1. StaggeredGridView 允许非对齐行的GridView 类似Pinterest的瀑布流,并且跟ListView一样自带View缓存,继承自ViewGroup 项目地址:https:/ ...
- python 替换windows换行符为unix格式
windows 默认换行符为 \r\n; unix默认换行符为 \n; 所以当win下编辑的脚本在linux下显示末尾多了^M: 换行符修改为同一的unix格式脚本如下: def run(path,f ...
- 一起啃PRML - 1.2.4 The Gaussian distribution 高斯分布 正态分布
一起啃PRML - 1.2.4 The Gaussian distribution 高斯分布 正态分布 @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ ...
- Redis 入门第一发
Redis 官网:http://redis.io/ 中文:http://www.redis.cn/topics/replication.html http://www.redis.cn ...
- Base64上传图片
#region 上传图片 [HttpPost]/// <summary>/// 上传文件 jpg图片/// </summary>/// <param name=" ...
- Asp.net--Ajax前后台数据交互
转自:http://www.cnblogs.com/guolebin7/archive/2011/02/22/1961737.html 代码由前后台两部分组成: 前台:(新建一个Default.asp ...