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,凸包入门!的更多相关文章

  1. 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 ...

  2. HDU - 1392 Surround the Trees (凸包)

    Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...

  3. hdu 1392 Surround the Trees (凸包)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. hdu 1392 Surround the Trees 凸包模板

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1392 Surround the Trees 凸包裸题

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 1392 Surround the Trees(凸包*计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...

  7. HDU 1392 Surround the Trees(凸包入门)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. hdu 1392:Surround the Trees(计算几何,求凸包周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDUJ 1392 Surround the Trees 凸包

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. Git .gitignore 设置为全局global

    在操作Git时,我们会将修改的内容$git add . 到Git,Git会提示我们哪些文件都修改了.此时提示中会包括系统自动修改的文件,bin文件等.而我们add到Git时,并不希望将这些文件也一同a ...

  2. 【数据库-Azure SQL Database】如何创建事务复制将本地数据同步到 SQL Azure

    Azure SQL DB 可以被配置成为 SQL Server 事务复制的一个订阅者( subscriber ). 主要应用场景有两种: 将您的数据迁移到 Azure SQL DB, 并且没有宕机时间 ...

  3. HDU 5469 Antonidas (树形DP,暴力)

    题意: 给一棵n节点的树图,每个点都是一个小写字母,要求找到两个点(a,b),从a->b的路径上形成了一个字符串为s.给出s,问是否存在这样的点对. 思路: 考虑一个点,要么从该点出发,要么在该 ...

  4. Kubenetes里pod和service绑定的实现方式

    我之前的文章 如何在Kubernetes里创建一个Nginx service介绍了如何创建一个Kubernetes pod和service,使用的方法是命令kubectl run. 本文介绍另一种方式 ...

  5. SQLite基础教程目录

    SQLite基础教程目录 SQLite主页 SQLite概述 SQLite -安装 SQLite -命令 SQLite -语法 SQLite -数据类型 SQLite -创建数据库 SQLite -附 ...

  6. 二级域名绑定ECS

    关于阿里云域名的绑定,下面是个人的理解,如有错误请指出. 首先,任何域名都需要在阿里云备案后才可以使用, 如果是二级域名,不能单独备案,需要其顶级域名在阿里云备案. http://help.aliyu ...

  7. Web项目之Django基础

    Django目录: python项目Django(web服务) python项目Django(HTTP协议) python项目Django(Django的安装与使用) python项目Django(U ...

  8. for..in...时,注意hasOwnProperty验证

    for..in...时,注意hasOwnProperty验证 var obj = { a: 10, b: 20 }; // 注意词句代码 Object.prototype.c = 30; var it ...

  9. Linux文件操作函数

    creat() 函数 close() 函数 read() 函数 read 函数实际读到的字节数少于要求读的字节数时: 读普通文件,在读到要求字节数之前就到达文件尾: 当从终端设备读,通常一次最多读一行 ...

  10. Linux网卡设置为网桥模式

    Linux网卡设置为网桥模式 1.    添加网卡,并修改相关配置文件 1.1虚拟机添加网卡,并配置相关文件 如:eth2为新添加网卡 cd /etc/sysconfig/network-script ...