POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 31414 | Accepted: 9749 |
Description
cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.
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
题意是给了二维坐标下的点,问在这些点中最远的两个点 距离的平方。
最远的两个点一定在凸包上,所以先找凸包上的点,在对凸包上的点求其距离平方找最大值。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n; struct no
{
int x, y;
}node[50005]; int conbag[50005];
no orign;//原点 int dis(no n1, no n2)
{
return (n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y);
} int xmult(int x1, int y1, int x2, int y2)
{
return x1*y2 - x2*y1;
} int Across(no n1, no n2, no n3, no n4)
{
return xmult(n2.x - n1.x, n2.y - n1.y, n4.x - n3.x, n4.y - n3.y);
} int cmp(const void* n1, const void* n2)//qsort和我原来想象的是完全相反的。。。
{
int temp = Across(orign, *(no *)n1, orign, *(no *)n2); if (temp > 0)
{
return -1;
}
else if (temp == 0)
{
return (dis(orign, *(no *)n1) - dis(orign, *(no *)n2));
}
else
{
return 1;
}
} int main()
{
int i, j, min_x, pos_x;
while (cin >> n)
{
min_x = 10005;
pos_x = 0; for (i = 1; i <= n; i++)
{
cin >> node[i].x >> node[i].y; if (node[i].x < min_x)
{
min_x = node[i].x;
pos_x = i;
}
else if (min_x == node[i].x&&node[i].y < node[pos_x].y)
{
pos_x = i;
}
}
orign = node[pos_x];
qsort(node + 1, n, sizeof(no), cmp); int pc = 1;
conbag[1] = 1;
conbag[++pc] = 2;
conbag[0] = 2; i = 3;
while (i <= n)
{
if (Across(node[conbag[pc - 1]], node[conbag[pc]], node[conbag[pc]], node[i]) >= 0)
{
conbag[++pc] = i++;
conbag[0]++;
}
else
{
pc--;
conbag[0]--;
}
}
int max_n = 0;
for (i = 1; i <= conbag[0]; i++)
{
for (j = i + 1; j <= conbag[0]; j++)
{
max_n = max(dis(node[conbag[i]], node[conbag[j]]), max_n);
}
}
cout << max_n << endl;
} return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离的更多相关文章
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...
- POJ 2187 Beauty Contest( 凸包求最远点对 )
链接:传送门 题意:给出 n 个点,求出这 n 个点中最远的两个点距离的平方 思路:最远点对一定会在凸包的顶点上,然后直接暴力找一下凸包顶点中距离最远的两个点 /******************* ...
- 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 (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26180 Accepted: 8081 D ...
- POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24283 Accepted: 7420 D ...
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- poj 2187:Beauty Contest(旋转卡壳)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 32708 Accepted: 10156 Description Bes ...
- poj 2187 Beauty Contest(二维凸包旋转卡壳)
D - Beauty Contest Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- bzoj 4475: [Jsoi2015]子集选取
233,扒题解的时候偷瞄到这个题的题解了,,GG 暴力发现是2^(nm),然后就是sb题了 #include <bits/stdc++.h> #define LL long long us ...
- Mybatis入门(五)属性名和字段名不一致解决
在学Mybatis的时候都需要创建一个实体类,但创建实体类的变量必须和数据库的一样,这章就来解决这个有趣的问题 目录: 问题是这样: 输出的结果是: password为空,这就很难受: 解决方法: 第 ...
- luogu P3357 最长k可重线段集问题
这题和3358一模一样,建模形式直接不用变,就两点不一样,一是len变化了,加入y后再更新即可,还有就是可能会出现x0=x1的情况,即一条开线段垂直x轴,如果我们依旧按照上一题的建图方法,就会出现负权 ...
- 小程序mdns+udpSocket实现电视推送
起因:公司以前小程序推送架构为:小程序->接口->后台->socket->机顶盒client->socket->后台->接口->小程序,一系列接口才得到 ...
- day07-Python运维开发基础(深/浅拷贝、字典/集合/相关操作)
1. 深拷贝与浅拷贝 # ### 深拷贝 和 浅拷贝 """ a = 7 b = a a = 8 print(b) lst1 = [1,2,3] lst2 = lst1 ...
- docker中mysql数据库
在docker中安装mysql数据库,直接上代码,pull 并run 补充20190809=============== 如果要挂载数据库实现数据持久化到本地的时候,会出现权限问题,这个原因是: 在执 ...
- oracle根据一张表更新另外一张表
知道是两张表进行更新,之前作过mysql的,直接就写了: update a,b set a.code = b.code wehre a.id = b.id 然后就报错了,上网查了下知道oracle不能 ...
- Python在运行中发生错误怎么正确处理方法,案例详解!
在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数open(),成功时返回文件描 ...
- C#知识点回顾随笔目录导航
此随笔只是春节期间回顾知识点,阅读<<C#学习笔记>>记录(2019-2-4至2019...); 使用有道云笔记记录可能会有些排版问题 思维导图预览(使用的有道云,无法截取完整 ...
- HTTP实战
1.建立http服务,要求: (1)提供两个基于名称的虚拟主机: www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1/erro ...