标签:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 29666 | Accepted: 9180 |
Description
Input
Output
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<math.h> #include<ctype.h> using namespace std; const int maxn=1000100; const int INF=(1<<28); const int EPS=0.00000001; int N; struct Point { int x,y; friend int operator*(Point A,Point B) { return A.x*B.y-A.y*B.x; } friend Point operator-(Point A,Point B) { return {A.x-B.x,A.y-B.y}; } }; Point farm[maxn]; Point ans_farm[maxn]; int ans_top; int dist2(Point A,Point B) { int tx=A.x-B.x,ty=A.y-B.y; return tx*tx+ty*ty; } bool cmpYX(const Point A,const Point B) { if(A.y<B.y) return true; if(A.y==B.y){ return A.x<=B.x; } return false; } bool cmp(const Point A,const Point B) { int tmp=(A-farm[0])*(B-farm[0]); return tmp>0; } void graham_scan() { sort(farm,farm+N,cmpYX); sort(farm+1,farm+N,cmp); ans_top=0; ans_farm[ans_top++]=farm[0]; ans_farm[ans_top++]=farm[1]; for(int i=2;i<N;i++){ if((ans_farm[ans_top-1]-ans_farm[ans_top-2])*(farm[i]-ans_farm[ans_top-1])<0){ ans_top--; ans_farm[ans_top++]=farm[i]; } else ans_farm[ans_top++]=farm[i]; } } int main() { while(cin>>N){ for(int i=0;i<N;i++){ scanf("%d%d",&(farm[i].x),&(farm[i].y)); } graham_scan(); int ans=0; for(int i=0;i<ans_top-1;i++){ for(int j=i+1;j<ans_top;j++){ int tmp=dist2(ans_farm[i],ans_farm[j]); if(tmp>ans) ans=tmp; } } printf("%d\n",(int)ans); } return 0; }
poj2187 求平面最远点对,garham_scan算法求凸包
标签:
原文地址:http://www.cnblogs.com/--560/p/4396997.html