2938. 周游世界

题目链接

2938. 周游世界

给定一个二维平面,平面上有 N 个点。

每个点的位置可由一对整数坐标 (x,y) 来表示,不同的点位置不同。

请你求出平面上距离最远的点对之间的距离是多少。

输入格式

第一行包含一个整数 N

接下来 N 行,每行包含两个整数 x,y,表示一个点的位置坐标。

输出格式

输出一个整数,表示距离最远的点对之间的距离的平方

数据范围

2N50000,
10000x,y10000

输入样例:

4 0 0 0 1 1 1 1 0

输出样例:

2

样例解释

第一个点和第三个点之间的距离最远,为 2

解题思路

旋转卡壳

很显然,答案肯定是在凸包上的两个点上,故不妨先用 andrew 算法求出凸包,一开始用两个平行线将凸包卡住,然后凸包开始往一个方向旋转一周,最后答案的那条线段会在两条平行线上出现,此方式正好对应对于凸包上的每条直线来说,找到一条最远的点,而每次往一个方向枚举直线时该最远的点具有单调性,即可以使用双指针算法解决该问题

  • 时间复杂度:O(nlogn)

代码

// Problem: 周游世界 // Contest: AcWing // URL: https://www.acwing.com/problem/content/2941/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> // #define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=5e4+5; int n; int stk[N],top; PII a[N]; PII operator-(PII a,PII b) { return {a.fi-b.fi,a.se-b.se}; } int operator*(PII a,PII b) { return a.fi*b.se-b.fi*a.se; } int area(PII a,PII b,PII c) { return (b-a)*(c-a); } int get_dist(PII a,PII b){ return (a.fi-b.fi)*(a.fi-b.fi)+(a.se-b.se)*(a.se-b.se); } void andrew() { sort(a+1,a+1+n); for(int i=1;i<=n;i++) { while(top>=2&&area(a[stk[top-1]],a[stk[top]],a[i])<=0)top--; stk[++top]=i; } int k=top; for(int i=n;i>=1;i--) { while(top>k&&area(a[stk[top-1]],a[stk[top]],a[i])<=0)top--; stk[++top]=i; } if(n>1)top--; } int rotating_calipers() { if(top<=2)return get_dist(a[1],a[n]); for(int i=0;i<top;i++)a[stk[i]]=a[stk[i+1]]; int res=0; for(int i=0,j=2;i<top;i++) { PII d=a[stk[i]],e=a[stk[i+1]]; while(area(d,e,a[stk[j]])<area(d,e,a[stk[(j+1)%top]]))j=(j+1)%top; res=max({res,get_dist(d,a[stk[j]]),get_dist(e,a[stk[j]])}); } return res; } int main() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i].fi>>a[i].se; andrew(); cout<<rotating_calipers(); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16918659.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示