UVa1453或La4728 凸包+枚举(或旋转卡壳)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4199
没想透为啥旋转卡壳跟枚举跑时间差不多。n太小吧!
枚举法:
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- const int maxn = ;
- const int maxe = ;
- const int INF = 0x3f3f3f;
- const double eps = 1e-;
- const double PI = acos(-1.0);
- struct Point{
- double x,y;
- Point(double x=, double y=) : x(x),y(y){ } //构造函数
- };
- typedef Point Vector;
- Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
- Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
- Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
- Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}
- bool operator < (const Point& a,const Point& b){
- return a.x < b.x ||( a.x == b.x && a.y < b.y);
- }
- int dcmp(double x){
- if(fabs(x) < eps) return ;
- else return x < ? - : ;
- }
- bool operator == (const Point& a, const Point& b){
- return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
- }
- ///向量(x,y)的极角用atan2(y,x);
- double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
- double Length(Vector A) { return Dot(A,A); }
- double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); }
- double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; }
- //凸包:
- /**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
- 点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/
- //Goal[]数组模拟栈的使用;
- int ConvexHull(Point* P,int n,Point* Goal){
- sort(P,P+n);
- int m = unique(P,P+n) - P; //对点进行去重;
- int cnt = ;
- for(int i=;i<m;i++){ //求下凸包;
- while(cnt> && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
- Goal[cnt++] = P[i];
- }
- int temp = cnt;
- for(int i=m-;i>=;i--){ //逆序求上凸包;
- while(cnt>temp && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
- Goal[cnt++] = P[i];
- }
- if(cnt > ) cnt--; //减一为了去掉首尾重复的;
- return cnt;
- }
- /*********************************分割线******************************/
- Point P[maxn*],Goal[maxn*];
- int n;
- int main()
- {
- //freopen("E:\\acm\\input.txt","r",stdin);
- int T;
- cin>>T;
- while(T--){
- cin>>n;
- int cnt = ;
- double x,y,w;
- for(int i=;i<=n;i++){
- scanf("%lf %lf %lf",&x,&y,&w);
- P[cnt++] = Point(x,y);
- P[cnt++] = Point(x+w,y);
- P[cnt++] = Point(x,y+w);
- P[cnt++] = Point(x+w,y+w);
- }
- cnt = ConvexHull(P,cnt,Goal);
- double Maxlen = ;
- for(int i=;i<cnt;i++)
- for(int j=i+;j<cnt;j++){
- Maxlen = max(Maxlen,Length(Goal[j]-Goal[i]));
- }
- printf("%.lf\n",Maxlen);
- }
- return ;
- }
旋转卡壳:
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- const int maxn = ;
- const int maxe = ;
- const int INF = 0x3f3f3f;
- const double eps = 1e-;
- const double PI = acos(-1.0);
- struct Point{
- double x,y;
- Point(double x=, double y=) : x(x),y(y){ } //构造函数
- };
- typedef Point Vector;
- Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
- Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
- Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
- Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}
- bool operator < (const Point& a,const Point& b){
- return a.x < b.x ||( a.x == b.x && a.y < b.y);
- }
- int dcmp(double x){
- if(fabs(x) < eps) return ;
- else return x < ? - : ;
- }
- bool operator == (const Point& a, const Point& b){
- return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
- }
- ///向量(x,y)的极角用atan2(y,x);
- double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
- double Length(Vector A) { return Dot(A,A); }
- double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); }
- double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; }
- //凸包:
- /**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
- 点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/
- // 如果不希望在凸包的边上有输入点,把两个 <= 改成 <
- //Goal[]数组模拟栈的使用;
- int ConvexHull(Point* P,int n,Point* Goal){
- sort(P,P+n);
- int m = unique(P,P+n) - P; //对点进行去重;
- int cnt = ;
- for(int i=;i<m;i++){ //求下凸包;
- while(cnt> && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
- Goal[cnt++] = P[i];
- }
- int temp = cnt;
- for(int i=m-;i>=;i--){ //逆序求上凸包;
- while(cnt>temp && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
- Goal[cnt++] = P[i];
- }
- if(cnt > ) cnt--; //减一为了去掉首尾重复的;
- return cnt;
- }
- //旋转卡壳可以用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离
- //计算凸包直径,输入凸包Goal,顶点个数为n,按逆时针排列,输出直径的平方
- double RotatingCalipers(Point* Goal,int n){
- double ret = ;
- Goal[n]=Goal[]; //补上使凸包成环;
- int pv = ;
- for(int i=;i<n;i++){ //枚举边Goal[i]Goal[i+1],与最远顶点Goal[pv];利用叉积求面积的方法求最大直径;;
- while(fabs(Cross(Goal[i+]-Goal[pv+],Goal[i]-Goal[pv+]))>fabs(Cross(Goal[i+]-Goal[pv],Goal[i]-Goal[pv])))
- pv = (pv+)%n;
- ret=max(ret,max(Length(Goal[i]-Goal[pv]),Length(Goal[i+]-Goal[pv+]))); //这个地方不太好理解,就是要考虑当pv与pv+1所在直线平行于i与i+1的情况;
- }
- return ret;
- }
- /*********************************分割线******************************/
- Point P[maxn*],Goal[maxn*];
- int n;
- int main()
- {
- //freopen("E:\\acm\\input.txt","r",stdin);
- int T;
- cin>>T;
- while(T--){
- cin>>n;
- int cnt = ;
- double x,y,w;
- for(int i=;i<=n;i++){
- scanf("%lf %lf %lf",&x,&y,&w);
- P[cnt++] = Point(x,y);
- P[cnt++] = Point(x+w,y);
- P[cnt++] = Point(x,y+w);
- P[cnt++] = Point(x+w,y+w);
- }
- cnt = ConvexHull(P,cnt,Goal);
- double Maxlen = RotatingCalipers(Goal,cnt);
- printf("%.lf\n",Maxlen);
- }
- return ;
- }
UVa1453或La4728 凸包+枚举(或旋转卡壳)的更多相关文章
- hdu 2187(凸包直径 1.枚举 2.旋转卡壳)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33115 Accepted: 10278 ...
- [Bzoj1069][Scoi2007]最大土地面积(凸包)(旋转卡壳)
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3629 Solved: 1432[Submit][Sta ...
- 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)
给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...
- [模板] 计算几何2: 自适应Simpson/凸包/半平面交/旋转卡壳/闵可夫斯基和
一些基本的定义在这里: [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算 自适应Simpson Simpson's Rule: \[ \int ^b_a f(x)dx\approx ...
- BZOJ 1185 [HNOI2007]最小矩形覆盖:凸包 + 旋转卡壳
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 题意: 给出二维平面上的n个点,问你将所有点覆盖的最小矩形面积. 题解: 先找出凸 ...
- 【BZOJ 1069】【SCOI 2007】最大土地面积 凸包+旋转卡壳
因为凸壳上对踵点的单调性所以旋转卡壳线性绕一圈就可以啦啦啦--- 先求凸包,然后旋转卡壳记录$sum1$和$sum2$,最后统计答案就可以了 #include<cmath> #includ ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- 【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖
1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1945 Solve ...
- HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015年百度之星程序设计大赛 - 初赛(1)
题目链接 题意:给出n个矩形,求能覆盖所有矩形的最小的矩形的面积. 题解:对所有点求凸包,然后旋转卡壳,对没一条边求该边的最左最右和最上的三个点. 利用叉积面积求高,利用点积的性质求最左右点和长度 ...
随机推荐
- postgresql sql修改表,表字段
1.更改表名 alter table 表名 rename to 新表名 2.更改字段名 alter table 表名 rename 字段名 to 新字段名 3.增加列 ALTER TABLE ud_w ...
- Const和ReadOnly
总结一下const和readonly有这么几条区别: const和readonly的值一旦初始化则都不再可以改写: const只能在声明时初始化:readonly既可以在声明时初始化也可以在构造器中初 ...
- 实现HTTP跳转到HTTPS
1 首先在您的网站下新建一个站点,名称随意,在属性中分配TCP端口为80,SSL不分配 然后在属性>主目录下配置 将此资源的内容来自: 改为 重定向到URL 然后重定向到中 输入: HTTP ...
- iOS自动布局之autoresizingi
对于iOS的app开发者来说,不会像Android开发者一样为很多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,但是从设计模式上来说这不是好的做法.而且也还有一些问题,如iPhone5的适配, ...
- SGU 200.Cracking RSA(高斯消元)
时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...
- 那些年,我们一起被坑的H5音频
原文地址:http://weibo.com/p/23041874d6cedd0102vkbr 不要被这么文艺的标题吓到,这里不会跟你讲述中学时期泡妞史,也不会有其它什么现实不该有而小说噼里啪啦不能 ...
- TestNG目录
1 - 简介 2 - 注解 3 - testng.xml 4 - 执行 TestNG 5 - 测试方法, 测试类 和 测试组 5.1 - 测试方法 5.2 - 测试组 5.3 ...
- centos7/RHEL7安装LibreOffice
1.下载 wget http://download.documentfoundation.org/libreoffice/testing/4.4.0/rpm/x86_64/LibreOfficeDev ...
- 批处理文件安装与卸载Windows服务
//安装Windows服务 将RECPost.exe和RECPostService替换成自己的项目名称和服务名称,并将文件保存成bat格式.其中%cd%是获取相对路径 @echo off set fi ...
- Smart Client Software Factory安装
首先要安装 Visual Studio 2010 SDK 不然无法安装 Smart Client Software Factory 2010 然后按顺序安装 GAX 2010 http://visua ...