#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,cnt;
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-'){t=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*t;
}
struct node
{
double x,y;
}p[50005],s[50005];
int dis(node a,node b){
return 1ll*(a.x-b.x)*(a.x-b.x)+1ll*(a.y-b.y)*(a.y-b.y);
}
double check(node a1,node a2,node b1,node b2)
{
return (a2.x-a1.x)*(b2.y-b1.y)-(b2.x-b1.x)*(a2.y-a1.y);
}
bool cmp(node p1,node p2)
{
double k=check(p[1],p1,p[1],p2);
if(k>0)return 1;
if(k==0&&dis(p[0],p1)<dis(p[0],p2))return 1;
return 0;
}
void find(){
sort(p+2,p+1+n,cmp);
s[1]=p[1];
cnt=1;
for(int i=2;i<=n;i++)
{
while(cnt>1&&check(s[cnt-1],s[cnt],s[cnt],p[i])<0)
cnt--;
cnt++;
s[cnt]=p[i];
}
s[0]=s[cnt];
}
int get(){
int ans=0;
if(cnt==2)return dis(s[0],s[1]);
int j=2;
for(int i=0;i<cnt;i++){
while(check(s[i],s[i+1],s[i],s[j])<check(s[i],s[i+1],s[i],s[j+1]))
j=(j+1)%cnt;
ans=max(ans,max(dis(s[i],s[j]),dis(s[i+1],s[j])));
}
return ans;
}
signed main()
{
n=read();
double mid;
for(int i=1;i<=n;i++)
{
p[i].x=read(),p[i].y=read();
if(p[i].y<p[1].y)
{
mid=p[1].y;p[1].y=p[i].y;p[i].y=mid;
mid=p[1].x;p[1].x=p[i].x;p[i].x=mid;
}
}
find();
printf("%lld",get());
return 0;
}
大无语,93pts 提交记录
然后改了个等号:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,cnt;
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-'){t=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*t;
}
struct node
{
double x,y;
}p[50005],s[50005];
double check(node a1,node a2,node b1,node b2)
{
return (a2.x-a1.x)*(b2.y-b1.y)-(b2.x-b1.x)*(a2.y-a1.y);
}
double gougu(node p1,node p2)
{
return sqrt((p2.y-p1.y)*(p2.y-p1.y)+(p2.x-p1.x)*(p2.x-p1.x));
}
bool cmp(node p1,node p2)
{
double k=check(p[1],p1,p[1],p2);
if(k>0)return 1;
if(k==0&&gougu(p[0],p1)<gougu(p[0],p2))return 1;
return 0;
}
void find(){
sort(p+2,p+1+n,cmp);
s[1]=p[1];
cnt=1;
for(int i=2;i<=n;i++)
{
while(cnt>1&&check(s[cnt-1],s[cnt],s[cnt],p[i])<=0)
cnt--;
cnt++;
s[cnt]=p[i];
}
s[0]=s[cnt];
}
int dis(node a,node b){
return 1ll*(a.x-b.x)*(a.x-b.x)+1ll*(a.y-b.y)*(a.y-b.y);
}
int get(){
int ans=0;
if(cnt==2)return dis(s[0],s[1]);
int j=2;
for(int i=0;i<cnt;i++){
while(check(s[i],s[i+1],s[i],s[j])<check(s[i],s[i+1],s[i],s[j+1]))
j=(j+1)%cnt;
ans=max(ans,max(dis(s[i],s[j]),dis(s[i+1],s[j])));
}
return ans;
}
signed main()
{
n=read();
double mid;
for(int i=1;i<=n;i++)
{
p[i].x=read(),p[i].y=read();
if(p[i].y<p[1].y)
{
mid=p[1].y;p[1].y=p[i].y;p[i].y=mid;
mid=p[1].x;p[1].x=p[i].x;p[i].x=mid;
}
}
find();
printf("%lld",get());
return 0;
}
变成86pts了(欲哭无泪) 记录详情
我在凸包的模板题也是这么写的,A了,这里为什么就不行了?
神仙大佬们,我何错之有啊?