旋转卡壳算法;

直接在这个上面粘的模板

主要用途:用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离···

这题就是求凸包的直径

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define eps 1e-9
using namespace std;
const double pi = acos(-); int dcmp(double x)
{
return fabs(x) < eps ? : (x > ? : -);
} struct Point
{
double x;
double y; Point(double x = , double y = ):x(x), y(y) {} bool operator < (const Point& e) const
{
return dcmp(x - e.x) < || (dcmp(x - e.x) == && dcmp(y - e.y) < );
} bool operator == (const Point& e) const
{
return dcmp(x - e.x) == && dcmp(y - e.y) == ;
}
}; typedef Point Vector; Vector operator + (Point A, Point B)
{
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Point A, Point B)
{
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Point A, double p)
{
return Vector(A.x * p, A.y * p);
} Vector operator / (Point A, double p)
{
return Vector(A.x / p, A.y / p);
}
double dot(Point a,Point b)
{
return a.x*b.x+a.y*b.y;
}
double cross(Point a,Point b)
{
return a.x*b.y-a.y*b.x;
}
Point rotate(Point a,double ang)
{
return Point(a.x*cos(ang)-a.y*sin(ang),a.x*sin(ang)+a.y*cos(ang));
}
int convexhull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=;
for(int i=; i<n; i++)
{
while(m>&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-; i>=; i--)
{
while(m>k&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
if(n>)m--;
return m;
}
bool onsegment(Point p,Point a,Point b)
{
return dcmp(cross(a-p,b-p))==&&dcmp(dot(a-p,b-p))<;
} bool SegmentProperIntersection( Point a1, Point a2, Point b1, Point b2 ) //线段相交,交点不在端点
{
double c1 = cross( a2 - a1, b1 - a1 ), c2 = cross( a2 - a1, b2 - a1 ),
c3 = cross( b2 - b1, a1 - b1 ), c4 = cross( b2 - b1, a2 - b1 );
return dcmp(c1)*dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
} int ispointinpolygon(Point p,int n,Point *poly)
{
int wn=;
for(int i=; i<n; i++)
{
if(onsegment(p,poly[i],poly[(i+)%n]))return -;
int k=dcmp(cross(poly[(i+)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+)%n].y-p.y);
if(k>&&d1<=&&d2>)wn++;
if(k<&&d2<=&&d1>)wn--;
}
if(wn!=)return ;
return ;
} bool Check(int n,Point *ch,int m,Point *th)
{
for(int i=; i<n; i++)
{
if(ispointinpolygon(ch[i],m,th)!=)return ;
}
for(int i=; i<m; i++)
if(ispointinpolygon(th[i],n,ch)!=)return ;
ch[n]=ch[];
th[m]=th[];
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(SegmentProperIntersection(ch[i],ch[i+],th[j],th[j+]))return ;
return ;
}
double rotating_calipers(Point *ch,int n)
{
int q=;
double ans=;
ch[n]=ch[];
for ( int i = ; i < n; ++i )
{
while ( cross( ch[i + ] - ch[i], ch[q + ] - ch[i] ) > cross( ch[i + ] - ch[i], ch[q] - ch[i] ) )
q = ( q + ) % n;
ans = max( ans, max( dot( ch[i]- ch[q],ch[i]-ch[q] ),dot( ch[i + ]-ch[q + ],ch[i + ]-ch[q + ] ) ));
}
return ans;
}
Point p[],ch[];
int main()
{
int n,m;
double x,y,w;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int cnt=;
for(int i=; i<n; i++)
{
scanf("%lf%lf%lf",&x,&y,&w);
p[cnt].x=x,p[cnt++].y=y;
p[cnt].x=x+w,p[cnt++].y=y;
p[cnt].x=x,p[cnt++].y=y+w;
p[cnt].x=x+w,p[cnt++].y=y+w;
}
int n1=convexhull(p,cnt,ch);
printf("%.0lf\n",rotating_calipers(ch,n1));
}
return ;
}

uva 1453 - Squares的更多相关文章

  1. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  2. UVa 201 Squares

    题意: 给出这样一个图,求一共有多少个大小不同或位置不同的正方形. 分析: 这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度 ...

  3. UVA 4728 Squares(凸包+旋转卡壳)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...

  4. 【每日一题】Squares UVA - 201 暴力+输出坑 + 读文件模板

    题意 给你n*n的图,让你数正方形 题解:暴力for每个点,对于每个点从它出发顺时针走一个正方形.走完就ans[i]++; 坑:多输了一行******,然后在那里手摸样例,无限debug orz #d ...

  5. 【UVA】201 Squares(模拟)

    题目 题目     分析 记录一下再预处理一下.     代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...

  6. Squares UVA - 201

    A children's board game consists of a square array of dots that contains lines connecting some of th ...

  7. UVa 1643 Angle and Squares

    题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...

  8. UVA 12113 Overlapping Squares

    题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8 ...

  9. UVa 1643 Angle and Squares (计算几何)

    题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...

随机推荐

  1. android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索

    我们的手机通讯录一般都有这样的效果,如下图: OK,这种效果大家都见得多了,基本上所有的android手机通讯录都有这样的效果.那我们今天就来看看这个效果该怎么实现. 一.概述 1.页面功能分析 整体 ...

  2. CentOS 6.6 yum源完全配置

    原文地址 http://blog.csdn.net/halazi100/article/details/41311837 一 yum 简介 yum,是"Yellow dog Updater, ...

  3. Ubuntu 14.04 忘记用户密码(备忘)

    参考文章地址:www.linuxidc.com/Linux/2013-11/92236.htm 重启电脑 开机就会进入一个Grub引导页面,选择 "Ubuntu 高级选项"之后,按 ...

  4. (转)了解了这些才能开始发挥jQuery的威力

    原文地址:http://www.cnblogs.com/dolphinX/p/3347677.html 由于当前jQuery如此的如雷贯耳,相信不用介绍什么是jQuery了,公司代码中广泛应用了jQu ...

  5. Apache 配置多端口 多虚拟主机 局域网访问

    \wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf 修改如下 NameVirtualHost *:80          Documen ...

  6. js广告浮动

    一个广告框在指定区域,有定位属性的父级区域内,一直向右向左移动,如果碰到左右边框,反向,如果碰到上下边距,反向,实现在指定框中浮动的效果. <!doctype html> <html ...

  7. SQL Server数据的导入导出

    SQL Server 2008的导入导出服务可以实现不同类型的数据库系统的数据转换.为了让用户可以更直观的使用导入导出服务,微软提供了导入导出向导.导入和导出向导提供了一种从源向目标复制数据的最简便的 ...

  8. 初次使用nodejs的问题

    使用npm install -g 'xxx' 之后仍然报 Cannot find module 'xxx' 错误,可以通过设置环境变量来解决: export NODE_PATH=/usr/local/ ...

  9. Bye 14 Hello 15

         打开博客.空间 窥探到大家都在写自己的2014,抬头一看日历2015已近在咫尺了,看着别人的成长(例如 今年看了多少书.做了什么项目.工资涨了多少.职位角色的变化.去了多少地方.还有一些发善 ...

  10. php安全模式

    http://www.cnblogs.com/samson/archive/2011/08/08/2130550.html php安全模式:safe_mode=on|off启用safe_mode指令将 ...