POJ 2187 Beauty Contest
来源:互联网 发布:马来西亚海关数据 编辑:程序博客网 时间:2024/06/05 23:30
题目链接:http://poj.org/problem?id=2187
题意:平面上有N个牧场,i号牧场的位置在格点(x,y),所有牧场的位置互不相同。请计算距离最远的两个牧场的距离,输出最远距离的平方
题解:裸凸包
先按x,y坐标升序排序
排序后的第一个和最后一个肯定是凸包上的点
它们之间可以分成上下两条链求解
在构造过程中加上新的点之后可能会破坏凸性,此时只要将凹的部分从末尾去除就好了
代码:
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <algorithm>#include <numeric>#include <functional>#define RI(N) scanf("%d",&(N))#define RII(N,M) scanf("%d %d",&(N),&(M))#define RIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))#define mem(a) memset((a),0,sizeof(a))using namespace std;const int inf=1e9;const int inf1=-1*1e9;typedef long long LL;double EPS=1e-10;double add(double a,double b){ if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0; else return a+b;}struct P{ double x,y; P() {} P(double x,double y) : x(x),y(y) {} P operator - (P p) { return P(add(x,-p.x),add(y,-p.y)); } P operator + (P p) { return P(add(x,p.x),add(y,p.y)); } double det(P p) { return x*p.y-y*p.x; }};P p[50005];bool cmp(P x,P y){ if(x.x==y.x) return x.y<y.y; else return x.x<y.x;}vector<P> convex(P *ps,int n){ sort(ps,ps+n,cmp); int k=0; vector<P> 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(P p1,P p2){ return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);}int main(){ int n; RI(n); P p[50005]; for(int i=0; i<n; i++) scanf("%lf %lf",&p[i].x,&p[i].y); vector<P> qs=convex(p,n); double res=-1; for(int i=0; i<qs.size(); i++) for(int j=i+1; j<qs.size(); j++) { res=max(res,dist(qs[i],qs[j])); } printf("%.0lf\n",res); return 0;}
0 0
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- Poj 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187--Beauty Contest
- poj 2187 Beauty Contest
- poj 2187 Beauty Contest
- hdu4717 The Moving Points 三分求函数最小值
- HTML5第六次培训(AJAX基础-http协议)
- Android传感器使用大全
- ajax在jquery中的应用
- C语言位操做符代码演示
- POJ 2187 Beauty Contest
- 我眼中的自动化测试框架设计要点-要让QA不必去写代码
- 《Java程序设计基础》 第8章手记Part 2
- Servlet过滤器
- hdu1051(贪心)
- (学习笔记6)BMP位图照片的灰度变换之直方图
- C++中的private/protected/public
- Python爬虫学习笔记二:百度贴吧网页图片抓取
- POJ2251 - Dungeon Master