[HDU1392]Surround the Trees
思路:
凸包模板题。
注意n=1和n=2的情况。
当n=1时,不需要绳子。
当n=2时,绳子长度为两棵树之间距离。
当n≥e时,Graham求凸包即可。最后将凸包上的所有相邻点距离求和。
- #include<cmath>
- #include<cstdio>
- #include<cctype>
- #include<algorithm>
- inline int getint() {
- char ch;
- while(!isdigit(ch=getchar()));
- int x=ch^'';
- while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
- return x;
- }
- struct Point {
- int x,y;
- Point operator - (const Point &x) const {
- return (Point){this->x-x.x,this->y-x.y};
- }
- int operator * (const Point &x) const {
- return this->x*x.y-x.x*this->y;
- }
- };
- int dist2(const Point x,const Point y) {
- return (x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y);
- }
- double dist(const Point x,const Point y) {
- return sqrt(dist2(x,y));
- }
- const int N=;
- Point p[N];
- bool operator < (const Point &p1,const Point &p2) {
- int s=(p1-p[])*(p2-p[]);
- return s<||(!s&&(dist2(p1,p[]))>=dist2(p2,p[]));
- }
- bool judgeOnLeft(int p0,int p1,int p2) {
- double s=(p[p1]-p[p0])*(p[p2]-p[p0]);
- return s<||(!s&&(dist2(p[p1],p[p0]))>=dist2(p[p2],p[p0]));
- }
- inline void swap(Point &a,Point &b) {
- Point t;
- t=a;
- a=b;
- b=t;
- }
- int main() {
- int n;
- while(n=getint()) {
- if(n==) {
- puts("0.00");
- continue;
- }
- if(n==) {
- Point a,b;
- a.x=getint(),a.y=getint();
- b.x=getint(),b.y=getint();
- printf("%.2f\n",dist(a,b));
- continue;
- }
- int k=;
- for(int i=;i<n;i++) {
- p[i].x=getint(),p[i].y=getint();
- if((p[i].x<p[k].x)||((p[i].x==p[k].x)&&(p[i].y<p[k].y))) k=i;
- }
- swap(p[],p[k]);
- std::sort(&p[],&p[n]);
- p[n]=p[];
- int s[N],top=;
- s[]=;
- s[]=;
- for(int i=;i<=n;i++) {
- while(top&&judgeOnLeft(s[top-],i,s[top])) top--;
- s[++top]=i;
- }
- s[top]=s[];
- double ans=;
- for(int i=;i<=top;i++) {
- ans+=dist(p[s[i]],p[s[i-]]);
- }
- printf("%.2f\n",ans);
- }
- return ;
- }
[HDU1392]Surround the Trees的更多相关文章
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- ACM学习历程—HDU1392 Surround the Trees(计算几何)
Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these ...
- hdu1392 Surround the Trees 凸包
第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行. 我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点. 然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点. #incl ...
- 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
[科普]什么是BestCoder?如何参加? Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- Surround the Trees[HDU1392]
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1392:Surround the Trees(凸包问题)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Surround the Trees(凸包求周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 ...
随机推荐
- 在全志平台调试博通的wifi驱动(类似ap6212)【转】
转自:http://blog.csdn.net/fenzhi1988/article/details/44809779 调试驱动之前,首先先看看驱动代码,了解代码大致工作流程,再根据硬件配置驱动,比如 ...
- 移植BOA服务器到开发板
移植BOA 服务器到GEC210 开发板 开发平台主机:VMWare--Ubuntu 10.04 LTS开发板:GEC210 / linux-2.6.35.7编译器:arm-linux-gcc-4.5 ...
- Sandcastle方法生成c#.net帮助类帮助文档chm
Sandcastle方法生成c#.net帮助类帮助文档即chm后缀的文档,其实是通过C#文档注释生成的XML文件来生成帮助文档的.因此,第一步就是生成XML文档, 步骤1生成XML文档 1.打开VS- ...
- C 长字符串换行方法
C中字符串有时候会出现很长的情况,如果不换行书写查看起来很不方便. 长字符串拆分成多行处理也是C规范的一部分. 方法1. 利用双引号" " ,将长字符串分成多个子串换行,C会自动无 ...
- jquery----用户密码验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- cf796d 树,bfs好题!
绝对是好题,把所有警察局放入队列然后开始广搜,如果碰到了vis过的顶点,但是那条边没有访问过,那么这条边就可以删掉 另外广搜的vis标记是在入队时就打的,, #include<bits/stdc ...
- python3 + selenium 之窗口切换
窗口切换 此代码来源学习后对淘宝操作实践记录: 以下代码在Chrome61和IE11上正常运行,Firefox5.7上运行存在一些问题须改进,应该是火狐不兼容差link_text部分和循环经常报错,在 ...
- 读取web.config和app.config配置文件
app.config: <add key="Password" value="123456"/> C#: string TQpwd ...
- OSGI命令
OSGi的一些支离破碎的知识 以下命令说明内容来自于Eclipse的OSGi框架Equinox. ---Controlling the OSGi framework---launch - start ...
- 使用SqlSugar 4.X的T4生成实体类
<#@ template debug="false" hostspecific="true" language="C#" #> ...