poj2187(凸包)
来源:互联网 发布:同花顺炒股交易软件 编辑:程序博客网 时间:2024/06/04 17:47
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 39026 Accepted: 12099
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.
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
* 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
40 00 11 11 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<stdio.h>#include<string.h>#include<math.h>#include<vector>#include<algorithm>using namespace std;struct Point{ double x,y; Point(){} Point(double _x,double _y){ x=_x; y=_y; } Point operator + (Point p){ return Point(x+p.x,y+p.y); } Point operator - (Point p){ return Point(x-p.x,y-p.y); } Point operator * (double d){ return Point(d*x,d*y); } double dot(Point p){ return x*p.x+y*p.y; } //内积 double det(Point p){ return x*p.y-p.x*y; } //外积 }ps[50005];int n;bool cmpxy(const Point &p,const Point &q){ //排序 if(p.x!=q.x) return p.x<q.x; return p.y<q.y; } vector<Point> convex_hull(Point *ps,int n){ sort(ps,ps+n,cmpxy); int k=0; vector<Point> qs(n*2); //构造中的凸包 for(int i=0;i<n;++i){ //构造凸包的下侧 while(k>1 && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0)--k; qs[k++]=ps[i]; } for(int i=n-2,t=k;i>=0;--i){ //构造凸包的上侧 while(k>t && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0)--k; qs[k++]=ps[i]; } qs.resize(k-1); return qs; } double dist(Point p,Point q){ //两点距离平方 return (p-q).dot(p-q); }void solve(){vector<Point> qs=convex_hull(ps,n);double res=0;for(int i=0;i<qs.size();i++)for(int j=0;j<i;j++)res=max(res,dist(qs[i],qs[j]));printf("%.0f\n",res);} int main(){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lf%lf",&ps[i].x,&ps[i].y);}solve();}
阅读全文
0 0
- poj2187(凸包)
- poj2187(凸包)
- poj2187(凸包)
- poj2187 凸包问题
- poj2187 凸包
- poj2187凸包
- POJ2187凸包
- POJ2187->凸包
- POJ2187--凸包--Beauty Contest
- poj2187 凸包直径模板
- poj2187 Beauty Contest 凸包
- Poj2187(最远点对距离)凸包+旋转卡壳
- poj2187 Beauty Contest 平面最远点对(凸包)
- [POJ2187]Beauty Contest(凸包+旋转卡壳)
- poj2187——Beauty Contest(凸包+旋转卡壳)
- POJ2187:Beauty Contest(凸包 & 旋转卡壳)
- POJ2187 Beauty Contest 凸包+旋转卡壳
- poj2187 Beauty Contest 凸包+旋转卡壳
- C语言的操作符
- IO Streams:字符流
- 【LeetNode2-1-5】Median of two sorted arrays--C++源代码(VS2015)
- 投资、投机、赌博、交易
- angular4实战(4)ngrx
- poj2187(凸包)
- ASP.NET增删改
- C++实现一个日期类
- 单例模式
- 测试系列-测试用例设计
- 【LeetNode2-1-6】Longest consecutive sequence--C++源代码(VS2015)
- AVL树的创建
- 插入最少的字符使字符串成为回文串
- 计数排序