Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 27507   Accepted: 8493

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

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<math.h>
#include<stdio.h>
using namespace std;
#define PI 3.1415926535898
struct point
{
int x,y;
};
point p[],res[];
int n,top;
int Dist(const point &arg1, const point &arg2)
{
return (arg1.x - arg2.x)*(arg1.x - arg2.x) + (arg1.y - arg2.y)*(arg1.y - arg2.y);
}
bool multi(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)>(p2.x-p0.x)*(p1.y-p0.y);
}
bool cmp(const point &a,const point &b)
{
point temp=p[];
double xmt=(a.x-temp.x)*(b.y-temp.y)-(b.x-temp.x)*(a.y-temp.y);
if(xmt) //向量不共线就按逆时针旋转
return xmt>;
return Dist(a,temp)>Dist(b,temp);//向量共线取最长的。
}
void graham()//p[0]是左下角的元素
{
res[]=p[];
sort(p+,p+n,cmp);//按照极角排序
res[]=p[];
res[]=p[];
top=; for(int i=; i<n; i++)
{
while(top>&&multi(p[i],res[top],res[top-]))
top--;
res[++top]=p[i];
}
}
int cross(point a,point b,point o)
{
return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
}
void rotating_calipers()
{
int q=,ans=;
res[++top]=res[];
for(int p=;p<n;p++)
{
while(cross(res[p+],res[q+],res[p])>cross(res[p+],res[q],res[p]))
q=(q+)%top;
ans=max(ans,max(Dist(res[p],res[q]),Dist(res[p+],res[q+])));
}
cout<<ans<<endl;
}
int main()
{
int i,mina,m;
point temp;
while(~scanf("%d",&n))
{
scanf("%d%d",&p[].x,&p[].y);
mina=;
for(i=; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[i].y<p[mina].y||(p[i].y==p[mina].y&&p[i].x<p[mina].x))
mina=i;
}
swap(p[].x,p[mina].x),swap(p[].y,p[mina].y);
graham();
rotating_calipers();
}
}

Beauty Contest 凸包+旋转卡壳法的更多相关文章

  1. POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]

    题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...

  2. POJ 2187 Beauty Contest [凸包 旋转卡壳]

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36113   Accepted: 11204 ...

  3. 【POJ 2187】Beauty Contest 凸包+旋转卡壳

    xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...

  4. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  5. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

  6. POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!

     凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...

  7. POJ2187 Beauty Contest(旋转卡壳)

    嘟嘟嘟 旋转卡壳模板题. 首先求出凸包. 然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了. 考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满 ...

  8. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  9. poj 2187 Beauty Contest(二维凸包旋转卡壳)

    D - Beauty Contest Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. css 弹性盒兼容性写法,直接复制粘贴

    看这个定义弹性布局盒子display:-webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; disp ...

  2. Java基础学习 —— io

    /** 解决数据与数据之间的传输问题. 字节流: 输入字节流: -------| InputStream 所有输入字节流的基类.抽象类. -----------| FileInputStream 读取 ...

  3. setAttribute设置无效

    我发现ie浏览器中动态用setAttribute设置style属性值始终不能设置,经过一番查找发现了这篇文字 http://webcenter.hit.edu.cn/articles/2009/05- ...

  4. Project 9:两种简单数列排序

    1.冒泡法排序 /* 冒泡排序法的核心思想就是依次把最大的数换到最后面. 若有n个数 就需要通过n-1次循环来排序. 具体做法就是从第一个数开始 两个数比较大小大的换到后面,这样最大的就在最后了. 然 ...

  5. Android studio 一些技术添加依赖,依赖库

    Recyclerview compile 'com.android.support:recyclerview-v7:21.0.+' butterKnife 的依赖compile 'com.jakewh ...

  6. 九度OJ 1016 火星A + B 未AC版,整型存储不下

    #include <iostream> #include <string.h> #include <sstream> #include <math.h> ...

  7. JAVA基础第八组(5道题)

    36. 37. 38.[程序38] 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. package com.niit.homework1; import java.u ...

  8. 201521123029《Java程序设计》第八周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 答: 2. 书面作业 本次作业题集集合 1.List中指定元素的删除( ...

  9. 201521123117 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点 2. 书面作业 Q1代码阅读:Child压缩包内源代码 1.com.parent包中Child.java文件能否编译通过?哪句会 ...

  10. 201521123092,《java程序设计》第1周学习总结

    1.本周学习总结 这一周是我学习java的第一周,刚接触一门全新的编程语言,觉得还是有点困难的,很多基础性的java知识需要一点点学习,我会请教同学以及查询网上的学习资料,认真学好这一门学科. 本周学 ...