POJ 2187 Beauty Contest

题意:

题目链接
给定 \(n\) 个点,求距离最远的两个点之间的距离,输出最远距离的平方 \(n<=50000\)

思路:

旋转卡壳。。。

注意事项:

数组名称不要弄混了

code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=50005;
int n,top,per[N],res;
struct point{int x,y;int dist(){return x*x+y*y;}}p[N],q[N];
point operator-(point x,point y){return (point){x.x-y.x,x.y-y.y};}
int operator^(point x,point y){return x.x*y.y-x.y*y.x;}
inline int read()
{
    int s=0,w=1; char ch=getchar();
    for(;'0'>ch||ch>'9';ch=getchar())if(ch=='-')w=-1;
    for(;'0'<=ch&&ch<='9';ch=getchar())s=(s<<1)+(s<<3)+(ch^48);
    return s*w;
}
inline bool cmp(int x,int y)
{
    int dit=(p[x]-p[1])^(p[y]-p[1]);
    if(dit)return dit>0;
    return (p[x]-p[1]).dist()<(p[y]-p[1]).dist();
}
inline void Graham()
{
    for(int i=2;i<=n;++i)
        if(p[1].x>p[i].x||(p[1].x==p[i].x&&p[1].y>p[i].y))
            swap(p[1],p[i]);
    for(int i=1;i<=n;++i)per[i]=i;
    sort(per+2,per+n+1,cmp);
    q[++top]=p[1];
    for(int i=2;i<=n;++i)
    {
        int j=per[i];
        while(top>1&&((p[j]-q[top-1])^(q[top]-q[top-1]))>=0)--top;
        q[++top]=p[j];
    }
    q[top+1]=q[1];
}
inline int nxt(int x){return x==top?1:x+1;}
inline int S(point x,point y){return abs(x^y);}
inline int solve()
{
    if(top==2) return (q[1]-q[2]).dist();
    for(int i=1,j=3;i<=top;++i)
    {
        while(i!=nxt(j)&&S(q[i]-q[j],q[i+1]-q[j])<=S(q[i]-q[j+1],q[i+1]-q[j+1]))
            j=nxt(j);
        res=max(res,(q[i]-q[j]).dist());
        res=max(res,(q[i+1]-q[j]).dist());
    }
    return res;
}
int main()
{
    n=read();
    for(int i=1;i<=n;++i)
        scanf("%d%d",&p[i].x,&p[i].y);
    Graham();
    printf("%d\n",solve());
    return 0;
}

原文地址:https://www.cnblogs.com/zmyzmy/p/12228303.html

时间: 2024-07-29 17:40:42

POJ 2187 Beauty Contest的相关文章

poj 2187 Beauty Contest——旋转卡壳

题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 https://www.jianshu.com/p/74c25c0772d6 可以再倒着枚举一遍那样求凸包. 用叉积算面积来旋转卡壳. 注意在面积等于的时候就不要往后走了,不然只有两个点的数据就会死循环. #include<cstdio> #include<cstring> #inclu

【POJ 2187】 Beauty Contest (凸包-Graham扫描算法)

[POJ 2187] Beauty Contest (凸包-Graham扫描算法) 找平面最远点对 数据很大 用暴力会T..我感觉-- 扫描出个凸包 然后枚举凸包上的点即可 没坑 int也可过 注意重边跟共线就行 代码下附赠几组数据 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include

poj 3660 Cow Contest(warshall算法)

poj 3660 Cow Contest Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the co

POJ 2187 旋转卡壳 + 水平序 Graham 扫描算法

水平序 Graham 扫描算法: 计算二维凸包的时候可以用到,Graham 扫描算法有水平序和极角序两种. 极角序算法能一次确定整个凸包, 但是计算极角需要用到三角函数,速度较慢,精度较差,特殊情况较多. 水平序算法需要扫描两次,但排序简单,讨论简单,不易出错. [算法流程] 1.对顶点按x为第一关键字,y为第二关键字进行排序. 2.准备一个空栈,并将前两个点压入栈. 3.对于每一个顶点A,只要栈顶中还至少两个顶点,记栈顶为T,栈中第二个为U. 若UT(向量) * TA(向量) <= 0, 则将

poj 2187 最远点对

题意: 给出n个点,求最远点对的距离. 限制: 2 <= n <= 5*1e4 思路: 凸包,旋转卡壳 /*poj 2187 题意: 给出n个点,求最远点对的距离. 限制: 2 <= n <= 5*1e4 思路: 凸包,旋转卡壳 */ #include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cmath> using

POJ 3360 H-Cow Contest

http://poj.org/problem?id=3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the comp

ACM: POJ 3660 Cow Contest - Floyd算法

链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Eac

poj 2187 N个点中输出2点的最大距离的平方

旋转卡壳 Sample Input 40 00 11 11 0Sample Output 2 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string> 6 # include <cmath> 7 # include <queue> 8 # define LL l

poj 2187

求凸包后枚举凸包上的点 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82