poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26180 | Accepted: 8081 |
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
Hint
Source
struct Point{
double x,y;
};
double dis(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
void graham(Point p[],int n) //点集和点的个数
{
int pl[];
//找到纵坐标(y)最小的那个点,作第一个点
int t = ;
for(int i=;i<=n;i++)
if(p[i].y < p[t].y)
t = i;
pl[] = t;
//顺时针找到凸包点的顺序,记录在 int pl[]
int num = ; //凸包点的数量
do{ //已确定凸包上num个点
num++; //该确定第 num+1 个点了
t = pl[num-]+;
if(t>n) t = ;
for(int i=;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
double x = xmulti(p[i],p[t],p[pl[num-]]);
if(x<) t = i;
}
pl[num] = t;
} while(pl[num]!=pl[]);
}
本题代码:
#include <iostream>
using namespace std;
struct Point{
int x,y;
}p[];
int xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int graham(Point p[],int n) //点集和点的个数
{
int pl[];
//找到纵坐标(y)最小的那个点,作第一个点
int t = ;
for(int i=;i<=n;i++)
if(p[i].y < p[t].y)
t = i;
pl[] = t;
//顺时针找到凸包点的顺序,记录在 int pl[]
int num = ; //凸包点的数量
do{ //已确定凸包上num个点
num++; //该确定第 num+1 个点了
t = pl[num-]+;
if(t>n) t = ;
for(int i=;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
int x = xmulti(p[i],p[t],p[pl[num-]]);
if(x<) t = i;
}
pl[num] = t;
} while(pl[num]!=pl[]);
//计算最长距离
int Max = ;
for(int i=;i<num-;i++)
for(int j=i+;j<=num-;j++){
int tt = (p[pl[i]].x-p[pl[j]].x)*(p[pl[i]].x-p[pl[j]].x) + (p[pl[i]].y-p[pl[j]].y)*(p[pl[i]].y-p[pl[j]].y);
//注意这里要用p[pl[i]],不能是p[i],否则会WA
if(tt>Max)
Max = tt;
}
return Max;
}
int main()
{
int n;
while(cin>>n){
for(int i=;i<=n;i++) //输入n个点
cin>>p[i].x>>p[i].y;
cout<<graham(p,n)<<endl; //输出最长距离
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 2187:Beauty Contest(计算几何,求凸包,最远点对)的更多相关文章
- POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24283 Accepted: 7420 D ...
- poj 2187 Beauty Contest(二维凸包旋转卡壳)
D - Beauty Contest Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 2187 Beauty Contest ——计算几何
貌似直接求出凸包之后$O(n^2)$暴力就可以了? #include <cstdio> #include <cstring> #include <string> # ...
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...
- ●POJ 2187 Beauty Contest
题链: http://poj.org/problem?id=2187 题解: 计算几何,凸包,旋转卡壳 一个求凸包直径的裸题,旋转卡壳入门用的. 代码: #include<cmath> # ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
随机推荐
- Unity3.5 GameCenter基础教程(转载)
原地址: http://forum.unity3d.com/threads/116901-Game-Center-Support/page3 using UnityEngine; using Unit ...
- 更轻便的markdown 编辑器Typora
更轻便的markdown 编辑器 Typora 所见即所得的键入方式 https://typora.io 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论.
- MAC OS X 10.8 操作远程SSH服务器 + 无密码输入使用SSH服务器
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://www.osssme.org/ 使用命令行连接连接远程SSH服务器,root为我使用的远程服务器用户名,@后为I ...
- shell脚本对多端口进程kill并重启进程
#!/bin/bash export LOG_FILE='/data/log/search' spider_search_pid=`ps aux|grep flask_web_search|grep ...
- STL容器分析--stack
stack,顾名思义,表示栈,先进后出.
- instanceof 与typeof的用法
通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型.例如: var oStringObject = new String("hello world"); con ...
- laravel文件存储Storage
use Illuminate\Support\Facades\Storage; //建立目录 Storage::disk('public')->makeDirectory(date('Y-m') ...
- C 语言 mmap
/* *@author cody *@date 2014-08-12 *@description */ /* #include <sys/mman.h> void *mmap(void * ...
- Excel 출력
NativeExcel 참조 사이트 http://www.nika-soft.com/dwnld.htm IWorkbook book = Factory.CreateWorkbook(); lWo ...
- Oauth2.0协议曝漏洞 大量社交网站隐私或遭泄露
2014年是IT业界不平常的一年,XP停服.IE长老漏洞(秘狐)等等层出不穷,现在,社交网络也爆出惊天漏洞:Oauth2.0协议漏洞 继OpenSSL漏洞后,开源安全软件再曝安全漏洞.新加坡南洋理工大 ...