POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
2015-09-24 16:21
483 查看
Beauty Contest
Description
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their
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
* Line 1: A single integer, N
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
Sample Output
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
题意是给了二维坐标下的点,问在这些点中最远的两个点 距离的平方。
最远的两个点一定在凸包上,所以先找凸包上的点,在对凸包上的点求其距离平方找最大值。
代码:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 31414 | Accepted: 9749 |
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their
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
* Line 1: A single integer, N
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
题意是给了二维坐标下的点,问在这些点中最远的两个点 距离的平方。
最远的两个点一定在凸包上,所以先找凸包上的点,在对凸包上的点求其距离平方找最大值。
代码:
#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; }
相关文章推荐
- hdu2955 Robberies(01背包)
- 白话经典算法系列之三 希尔排序的实现
- Android虚拟机器学习总结Dalvik虚拟机创建进程和线程分析
- Java追加文件内容的三种方法
- POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
- IIS 7.5 .Net 4.5 WCF 404.3错误
- 谷歌还会出VP10还是放弃?
- Android5.0之CardView使用教程
- C++中的string常用函数用法总结
- Mahout算法集[机器学习算法]
- 【容斥原理-求区间内与n互质的数】HDOJ Co-prime 4135
- C# 将PowerPoint文件转换成PDF文件
- HTTP协议的请求和响应学习
- [转] javascript对数组的操作
- hdu 2191 (多重背包+二进制优化)
- mydumper使用
- canvas(3)阴影和渐变
- Android 图片二次采样、质量压缩
- Linux C pipe函数 man 翻译
- HDU 5033 Building(单调栈维护凸包)