题目链接
如图计算上凸包时判断
C
1
,
C
2
,
C
3
C_1,C_2,C_3
C1,C2,C3显然
C
3
C_3
C3是经过
A
,
B
A,B
A,B的上凸包
可以发现只需要通过
A
,
B
A,B
A,B即可判断,通过观察可以发现
C
3
y
−
A
y
C
3
x
−
A
x
<
=
B
y
−
A
y
B
x
−
A
x
\frac{C_{3y}-A_{y}}{C_{3x}-A_x}<=\frac{B_y-A_y}{B_x-A_x}
C3x−AxC3y−Ay<=Bx−AxBy−Ay时为上凸包
即
(
C
3
y
−
A
y
)
∗
(
B
x
−
A
x
)
−
(
C
3
x
−
A
x
)
∗
(
B
y
−
A
y
)
<
=
0
(C_{3y}-A_y)*(B_x-A_x)-(C_{3x}-A_x)*(B_y-A_y)<=0
(C3y−Ay)∗(Bx−Ax)−(C3x−Ax)∗(By−Ay)<=0
但需注意有些数据需要去重之后相等的情况才可以算作弹出情况
计算下凸包时可利用上凸包的来算,因为可将q[t-1]作为相对原点的位置
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#define all(a) a.begin(),a.end()
using namespace std;
using ll = long long ;
struct node{
double x,y;
bool operator<(const node&p)const{
return x==p.x?y<p.y:x<p.x;
}
node operator-(const node&p)const{
return {x-p.x,y-p.y};
}
double operator*(const node&p)const{///cross
return x*p.y-y*p.x;
}
bool operator==(const node&p)const{
return x==p.x&&y==p.y;
}
};
double dis(node &a,node &b){
return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
bool vis[10010];
int q[10010];
signed main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
vector<node>a(n);
for(auto &[x,y]:a) cin>>x>>y;
sort(all(a));
//a.erase(unique(a.begin(),a.end()),a.end());
int t=0;
for(int i=0;i<a.size();i++){///up convex hull
while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0) vis[q[t--]]=false;
q[++t]=i;
vis[i]=true;
}
vis[0]=false;
for(int i=a.size()-1;i>=0;i--){
if(vis[i])continue ;
while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0)t--;
q[++t]=i;
}
double ans=0;
for(int i=1;i<t;i++)
ans+=dis(a[q[i]],a[q[i+1]]);
cout<<fixed<<setprecision(2)<<ans;
}
题目链接
计算旋转卡壳时必须采用面积法,不可采用点点间因为点点之间距离不一定是连续的
如一条平行线与圆相交有两个点时,距离可能会先减小后增大
while(abs(area(a[q[i]],a[q[i%t+1]],a[q[j]]))<abs(area(a[q[i]],a[q[i%t+1]],a[q[j%t+1]])))j=j%t+1;
ans=max(ans,dis(a[q[i]],a[q[j]]));
ans=max(ans,dis(a[q[i%t+1]],a[q[j]]));
ans=max(ans,dis(a[q[i%t+1]],a[q[j%t+1]]));
ans=max(ans,dis(a[q[i]],a[q[j%t+1]]));
此处需要四次取最大值前两次取最大值显而易见,后两次是当两个area相等时不代表dis(a[q[i]],a[q[j]])一定比dis(a[q[i%t+1]],a[j%t+1])大
#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
struct node{
ll x,y;
bool operator<(const node&p)const{
return x==p.x?y<p.y:x<p.x;
}
node operator-(const node&p)const{
return {x-p.x,y-p.y};
}
ll operator*(const node&p)const{
return x*p.y-y*p.x;
}
}a[100010];
ll dis(const node&a,const node&b){
return pow(a.x-b.x,2)+pow(a.y-b.y,2);
}
int q[100010];
bool vis[100010];
ll area(const node&a,const node&b,const node&c){
return (a-b)*(a-c);
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i].x>>a[i].y;
sort(a,a+n);
int t=0;
for(int i=0;i<n;i++){
while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0)vis[q[t--]]=false;
q[++t]=i;
vis[i]=true;
}
vis[0]=false;
for(int i=n-1;i>=0;i--){
if(vis[i])continue;
while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0)t--;
q[++t]=i;
}
ll ans=0;
for(int i=1,j=1;i<=t;i++){
while(abs(area(a[q[i]],a[q[i%t+1]],a[q[j]]))<abs(area(a[q[i]],a[q[i%t+1]],a[q[j%t+1]])))j=j%t+1;
ans=max(ans,dis(a[q[i]],a[q[j]]));
ans=max(ans,dis(a[q[i%t+1]],a[q[j]]));
ans=max(ans,dis(a[q[i%t+1]],a[q[j%t+1]]));
ans=max(ans,dis(a[q[i]],a[q[j%t+1]]));
}
cout<<ans;
}