社区讨论
WA#9求助
P1452【模板】旋转卡壳 / [USACO03FALL] Beauty Contest G参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mi861rke
- 此快照首次捕获于
- 2025/11/21 09:13 4 个月前
- 此快照最后确认于
- 2025/11/21 09:13 4 个月前
RT
CPP// luogu-judger-enable-o2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-8;
const int N=500010;
inline int dcmp(double x) {
if (fabs(x)<=eps) {
return 0;
}
return x<0?-1:1;
}
struct point {
double x;
double y;
point(){}
point(double u,double v) {
x=u;
y=v;
}
bool operator < (const point &rhs) const {
return dcmp(x-rhs.x)<0||(dcmp(x-rhs.x)==0&&dcmp(y-rhs.y)<0);
}
};
point operator + (point A,point B) {
return point(A.x+B.x,A.y+B.y);
}
point operator - (point A,point B) {
return point(A.x-B.x,A.y-B.y);
}
point operator * (point A,double p) {
return point(A.x*p,A.y*p);
}
point operator * (double p,point A) {
return point(A.x*p,A.y*p);
}
double operator * (point A,point B) {
return (A.x*B.y)-(A.y*B.x);
}
point operator ^ (point A,point B) {
return point(A.x*B.x,A.y*B.y);
}
inline double dis(point A,point B) {
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
bool operator == (point A,point B) {
return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;
}
point p[N];
int n;
point sta[N];
int top;
double ans;
bool same_x=1,same_y=1;
inline bool cmpx(point A,point B) {
return A.y<=B.y;
}
inline bool cmpy(point A,point B) {
return A.x<=B.x;
}
inline void init() {
for(int i=0;i<n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(int i=1;i<n;i++) {
if (dcmp(p[i+1].x-p[i].x)!=0) {
same_x=0;
}
if (dcmp(p[i+1].y-p[i].y)!=0) {
same_y=0;
}
}
if (same_x) {
sort(p,p+n,cmpx);
ans=p[n-1].y-p[0].y;
return;
}
if (same_y) {
sort(p,p+n,cmpy);
ans=p[n-1].x-p[0].x;
return;
}
sort(p,p+n);
n=unique(p,p+n)-p;
}
inline int Graham() {
top=0;
for(int i=0;i<n;i++) {
while(top>1&&(sta[top-1]-sta[top-2])*(p[i]-sta[top-2])<0) {
top--;
}
sta[top++]=p[i];
}
int k=top;
for(int i=n-2;i>=0;i--) {
while(top>k&&(sta[top-1]-sta[top-2])*(p[i]-sta[top-2])<0) {
top--;
}
sta[top++]=p[i];
}
if (n>1) {
top--;
}
return top;
}
inline double get_diameter() {
double d=0;
if (top==2) {
return dis(sta[0],sta[1]);
}
sta[++top]=sta[0];
int j=2;
for(int i=0;i<top;i++) {
while((sta[i+1]-sta[i])*(sta[j]-sta[i])<(sta[i+1]-sta[i])*(sta[j+1]-sta[i])) {
j=(j+1)%top;
}
d=max(d,max(dis(sta[i],sta[j]),dis(sta[i+1],sta[j])));
}
return d;
}
int main() {
scanf("%d",&n);
init();
int s=Graham();
if (same_x||same_y) {
printf("%.0lf\n",ans*ans);
return 0;
}
ans=get_diameter();
printf("%.0lf\n",ans*ans);
return 0;
}
output:53020169
std_ans:56552480
回复
共 2 条回复,欢迎继续交流。
正在加载回复...