求凸包+旋转卡壳
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
struct poi
{
int x,y;
} a[1000005];
int b[1000005];
int o;
int cj(int a,int b,int c,int d)
{
return a*d-b*c;
}
int pd(poi a,poi b,poi c)
{
return cj(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
bool cmp(poi a,poi b)
{
if(a.y!=b.y)return a.y<b.y;
return a.x<b.x;
}
void graham()
{
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
{
while(o>=2&&pd(a[b[o]],a[b[o-1]],a[i])>=0)
o--;
o++;
b[o]=i;
}
int tmp=o;
for(int i=n;i>=1;i--)
{
while(o>tmp&&pd(a[b[o]],a[b[o-1]],a[i])>=0)
o--;
o++;
b[o]=i;
}
return ;
}
long long dis(poi a,poi b)
{
return 1ll*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
graham();
if(n>1)o--;
int now=2;
long long maxx=0ll;
if(o==2)
{
printf("%lld",dis(a[b[1]],a[b[2]]));return 0;
}
b[o+1]=b[1];
for(int i=1;i<=o;i++)
{
int x,y,xx,yy;
x=a[b[i+1]].x-a[b[i]].x;
y=a[b[i+1]].y-a[b[i]].y;
while(cj(x,y,a[b[now]].x-a[b[i]].x,a[b[now]].y-a[b[i]].y)<cj(x,y,a[b[now+1]].x-a[b[i]].x,a[b[now+1]].y-a[b[i]].y))
{
now++;
if(now==o+1)now=1;
}
maxx=max(maxx,max(dis(a[b[now]],a[b[i]]),dis(a[b[now+1]],a[b[i+1]])));
}
printf("%lld",maxx);
return 0;
}
半平面交
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
struct poi
{
double x,y;
poi(){}
poi(double x,double y):x(x),y(y)
{
}
}a[55];
struct Line
{
poi a,b;
double angle;
Line(){}
Line(poi a,poi b):a(a),b(b)
{
angle=atan2(b.y,b.x);
}
};
poi operator +(const poi &a,const poi &b)
{
return poi(a.x+b.x,a.y+b.y);
}
poi operator -(const poi &a,const poi &b)
{
return poi(a.x-b.x,a.y-b.y);
}
poi operator *(const double &a,const poi &b)
{
return poi(b.x*a,b.y*a);
}
double operator *(const poi &a,const poi &b)
{
return double(a.x*b.y-a.y*b.x);
}
poi operator |(const Line &a,const Line &b)
{
double percent=1.00000*(b.b*(a.a-b.a))/(a.b*b.b);
//cout<<percent<<endl;
return poi(a.a.x+percent*a.b.x,a.a.y+percent*a.b.y);
}
int edge;
Line ll[505];
int q[505];
poi ans[505];
int cnt;
int onleft(Line a,Line b,Line c)
{
//cout<<a.a.x<<" "<<a.a.y<<" "<<a.b.x<<" "<<a.b.y<<" "<<b.a.x<<" "<<b.a.y<<" "<<b.b.x<<" "<<b.b.y<<" "<<c.a.x<<" "<<c.a.y<<c.b.x<<" "<<c.b.y<<endl;
return c.b*((a|b)-c.a)<0;
}
bool cmp(Line a,Line b)
{
if(fabs(a.angle-b.angle)<1e-10)return a.b*(b.a-a.a)<0;
return a.angle<b.angle;
}
int main()
{
scanf("%d",&n);
int m;
for(int i=1;i<=n;i++)
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
if(i!=1)ll[++edge]=Line(a[i-1],a[i]-a[i-1]);
}
ll[++edge]=Line(a[m],a[1]-a[m]);
}
sort(ll+1,ll+1+edge,cmp);cnt=1;
for (int i=2;i<=edge;i++)
if(fabs(ll[i].angle-ll[cnt].angle)>1e-10) ll[++cnt]=ll[i];
edge=cnt;
int l=1,r=0;
q[++r]=1;
q[++r]=2;
cnt=0;
for(int i=3;i<=edge;i++)
{
while(l<r&&onleft(ll[q[r-1]],ll[q[r]],ll[i]))r--;
while(l<r&&onleft(ll[q[l+1]],ll[q[l]],ll[i]))l++;
q[++r]=i;
}
while(l<r&&onleft(ll[q[r-1]],ll[q[r]],ll[q[l]]))r--;
while(l<r&&onleft(ll[q[l+1]],ll[q[l]],ll[q[r]]))l++;
for(int i=l;i<r;i++)
ans[++cnt]=ll[q[i]]|ll[q[i+1]];
ans[++cnt]=ll[q[l]]|ll[q[r]];
double tot=0.00;
if(cnt<=2)
{
printf("0.000");
return 0;
}
ans[cnt+1]=ans[1];
for(int i=1;i<=cnt;i++)
tot+=ans[i]*ans[i+1];
printf("%.3lf",tot/2);
return 0;
}
没了~~~