HDU-1392 Surround the Trees,凸包入门!
Surround the Trees
此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点。
题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内。
思路:简单凸包入门题,凸包裸模板。在做这个题前建议先去学学:叉积,极角排序,三角形有向面积。
贴上代码以后再复习。
struct node
{
double x,y;
} a[N],p[N];
int n,tot;//总点数和凸包上的点数;
double dis(node a,node b)//两点间距离
{
return hypot(a.x-b.x,a.y-b.y);
}
//向量叉积(x1*y2-x2*y1);为正在直线右边(逆时针排列),为负在直线左边,为0共线;
int multi(node p0,node p1,node p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int cmp(node p1,node p2)
{
int x=multi(p1,p2,a[0]);
if(x>0||(x==0&&dis(p1,a[0])<dis(p2,a[0]))) return 1;
return 0;
}
void Graham()
{
int k=0;
for(int i=1; i<n; i++) //找最下面且最靠左的点;
if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
k=i;
swap(a[0],a[k]);
sort(a+1,a+n,cmp);//极角排序确定点的顺序;
if(n==1)
{
printf("0.00\n");
return ;
}
if(n==2)
{
printf("%.2f\n",dis(a[0],a[1]));
return ;
}
tot=2,p[0]=a[0],p[1]=a[1];
for(int i=2; i<n; i++)
{
while(tot>1&&multi(a[i],p[tot-1],p[tot-2])>=0) tot--;
p[tot++]=a[i];
}
double diss=0;
for(int i=0; i<tot; i++) diss+=dis(p[i],p[(i+1)%tot]);
printf("%.2f\n",diss);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for(int i=0; i<n; i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
Graham();
}
return 0;
} /*****************************************
***************** LYQ ***************
***************** YES ***************
*UserID: secrecy *
*RunOJ: *
*RunID: *
*Submit time: *
*Language: G++ *
*Result: Accepted *
*time: *
*Memory: *
*Length: *
*School: NYIST *
*Blog: http://blog.csdn.net/nyist_tc_lyq *
*QQ: *
*Tel: *
*****************************************/
人生第一个凸包算法,Good job!
HDU-1392 Surround the Trees,凸包入门!的更多相关文章
- HDU 1392 Surround the Trees (凸包周长)
题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...
- 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 ...
- HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- 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 ...
- HDUJ 1392 Surround the Trees 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- iOS之核心动画
.将动画的所有方法封装到一个类里面 MyCAHelper.h #import <Foundation/Foundation.h> #import <QuartzCore/Quartz ...
- hadoop中修改端口号
1.hdfs-site.xml 这里修改hdfs相关的端口. 1 <property> 2 <name>dfs.namenode.scondary.http-address&l ...
- 自己开发的在线视频下载工具,基于Java多线程
比如这个在线视频: 我们可以正常播放,但是找不到下载按钮. 打开Chrome开发者工具,在Network标签页里能看到很多网络传输请求: 随便看一个请求的响应,发现类型为video,大小为500多k. ...
- 解决activeandroid no such table
场景:activeandroid拷贝数据库 (1)复制sql数据库到项目的assets目录,例如/myapp/src/main/assets/prepop.db (2)确保manifest的AA_DB ...
- UVA - 1395 Slim Span (最小生成树Kruskal)
Kruskal+并查集. 点很少,按边权值排序,枚举枚举L和R,并查集检查连通性.一旦连通,那么更新答案. 判断连通可以O(1),之前O(n)判的,第一次写的过了,后来T.. #include< ...
- Gym 100883J palprime(二分判断点在凸包里)
题意:判断一堆小点有多少个在任意三个大点构成的三角形里面. 思路:其实就是判断点在不在凸包里面,判断的话可以使用二分来判断,就是判断该点在凸包的哪两个点和起点的连线之间. 代码: /** @xigua ...
- 分布式文件系统ceph介绍
ceph哲学思想 1. 每个组件必须支持扩展 2.不存在单点故障 3.解决方案必须是基于软件的.开源的.适应能力强 4.任何可能的一切必须自我管理 存在的意义:帮助企业摆脱昂贵的专属硬件 ceph目标 ...
- win10搭建Java环境
一.下载地址 jdk和jre官方网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 根据你的系统选择你需要 ...
- Open Cascade:AIS_InteractiveContext如何调用函数选择AIS对象
AIS_InteractiveContext如何调用函数选择AIS对象 myAISContext->MoveTo(point.x, point.y, myView); myAISContext- ...
- C02 信息存储与运算
目录 计算机内存 常量和变量 数据类型 运算符 计算机内存管理 计算机内存 信息存储概述 使用程序进行开发时,需要存储各种信息,这时候就需要用到变量.由于信息类型不同,变量的类型也因此不尽相同. 同时 ...