Beauty Contest(graham求凸包算法)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 25256 | Accepted: 7756 |
Description
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.
Input
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2 题意:给n个点的坐标,计算这些点两两距离最大的那个距离; 思路:如果直接枚举,肯定超。所以可以先形成凸包,距离最大的那两个端点一定是凸包中的点。所以形成凸包后再枚举就可以了。
这里用了graham算法,
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = ;
int top,stack[maxn];
int n;
struct Point
{
double x;
double y;
} point[maxn]; double cross(const Point &a, const Point &b, const Point &c)//三个点的叉积,结果大于0说明bc的极角大于ac的极角,等于0说明共线;
{
return (a.x-c.x)*(b.y-c.y) - (a.y-c.y)*(b.x-c.x);
} double dis(const Point &a, const Point &b)
{
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
} int cmp(const Point &a, const Point &b)//对点排序找出最左最下的那个点作为point[0];
{
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
} void Graham()
{
sort(point,point+n,cmp);
for(int i = ; i <= ; i++)
stack[i] = i;
top = ;
for(int i = ; i < n; i++)
{
while(top && cross(point[i],point[stack[top]],point[stack[top-]]) >= )
top--;
stack[++top] = i;
}
int count = top;
stack[++top] = n-;
for(int i = n-; i >= ; i--)
{
while(top != count && cross(point[i],point[stack[top]],point[stack[top-]])>=)
top--;
stack[++top] = i;
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i = ; i < n; i++)
scanf("%lf %lf",&point[i].x,&point[i].y); Graham(); double ans = ,distance;
for(int i = ; i < top; i++)
{
for(int j = ; j < i; j++)
{
distance = dis(point[stack[i]],point[stack[j]]);
if(ans < distance)
ans = distance;
}
}
printf("%.0lf\n",ans);
}
return ;
}
这是经典的计算几何学问题,判断向量p1=(x1,y1)到p2=(x2,y2)是否做左转,只需要判断x1*y2-x2*y1的正负,如果结果为正,则从p1到p2做左转。也就是向量的叉积。
Graham算法是这样的
1.将各点排序(),为保证形成圈,把P0在次放在点表的尾部;
2.准备堆栈:建立堆栈S,栈指针设为t,将0、1、2三个点压入堆栈S;
3.对于下一个点i
只要S[t-1]、S[t]、i不做左转
就反复退栈;
将i压入堆栈S
4.堆栈中的点即为所求凸包;
Beauty Contest(graham求凸包算法)的更多相关文章
- POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24283 Accepted: 7420 D ...
- Graham Scan凸包算法
获得凸包的算法可以算是计算几何中最基础的算法之一了.寻找凸包的算法有很多种,Graham Scan算法是一种十分简单高效的二维凸包算法,能够在O(nlogn)的时间内找到凸包. 首先介绍一下二维向量的 ...
- poj 2187 Beauty Contest(二维凸包旋转卡壳)
D - Beauty Contest Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 1113 Wall(Graham求凸包周长)
题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...
- HDU 1392 Surround the Trees (Graham求凸包周长)
题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...
- POJ2187 Beauty Contest (旋转卡壳算法 求直径)
POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...
- Graham求凸包模板
struct P { double x, y; P(, ):x(x), y(y) {} double add(double a, double b){ ; return a+b; } P operat ...
- (模板)graham扫描法、andrew算法求凸包
凸包算法讲解:Click Here 题目链接:https://vjudge.net/problem/POJ-1113 题意:简化下题意即求凸包的周长+2×PI×r. 思路:用graham求凸包,模板是 ...
- nyoj-78-圈水池(Graham算法求凸包)
题目链接 /* Name:nyoj-78-圈水池 Copyright: Author: Date: 2018/4/27 9:52:48 Description: Graham求凸包 zyj大佬的模板, ...
随机推荐
- [转] SSH 密钥认证机制
使用 RSA 密钥对进行 SSH 登录验证 使用 RSA 密钥对验证 SSH 的优点是 1) 不用打密码 2) 比密码验证更安全:缺点是 1) 第一次配置的时候有点麻烦 2) 私钥需要小心保存.Any ...
- Android 6.0 Changes
原文链接:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html 伴随着新特性和功能,Andr ...
- 修改android应用包名 分类: android 学习笔记 2015-07-16 22:48 4人阅读 评论(0) 收藏
由于项目需要,要修改已经开发好的应用包名,这本身很简单,但是如果你没找到门道,可能会白白浪费许多时间. 修改包名有三个地方要改,这三个地方的修改一定要按顺序来,否则你可能会遇到许多不必要的麻烦. 1. ...
- maven占位符
maven占位符默认是${} 也可以自己指定. pom.xml配置如下: <plugin> <groupId>org.apache.maven.plugins</grou ...
- 调优系列-tomcat调优
http://www.360doc.com/content/14/1208/13/16070877_431273418.shtml 使用JMeter对Tomcat进行压力测试与Tomcat性能调优 n ...
- Axure RP 8.0正式版下载地址 安装和汉化说明
1.Axure RP和中文包包下载地址 官网地址:http://www.axure.com.cn/3510/ 2.下载完成后安装 3.破解 axure8.0注册码激活码:(亲测可用)用户名:aaa注册 ...
- 动软代码生成器三层用于winform
DBUtility项目中的DbHelperSQL.cs (找自己对应的数据库类型) 修改前20行中的数据库连接字符串获取方式为: //数据库连接字符串(web.config来配置),多数据库可使用Db ...
- Spring 实例化bean的方式
实例化bean的方式有三种: 1.用构造器来实例化 2.使用静态工厂方法实例化 3.使用实例工厂方法实例化 当采用构造器来创建bean实例时,Spring对class并没有特殊的要求, 我们通常使用的 ...
- 初试ubuntu14.4问题集锦2
好的,我开始继续鼓捣. 想了这么长时间,我想到的是,肯定是compiz设置了unity的什么东西才导致这种问题,绝非什么显卡驱动的事情. 于是二话不说,开机登录,进入tty1,然后apt-get re ...
- SGU 106.Index of super-prime
时间限制:0.25s 空间限制:4M 题目大意: 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...