题意:给点N棵树,前K棵是已经拥有的,现在可以再拥有一棵树,问形成的最大凸包面积。
思路:先求K棵树的凸包C,然后对于后面的N-K棵树,我们先判断是否在凸包内,如果不在,我们要求两个切线。 这里分类讨论,即可。
如果点在C的左边,那么两条切线分别一上一下; 如果在下边,两条切线一左一右。 然后去对应区间二分即可。
(好像还有双指针的线性做法:求两个凸包,维护两条切线即可。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct point{
ll x,y;
point(){}
point(ll xx,ll yy):x(xx),y(yy){}
};
bool cmp(point w,point v){
if(w.x!=v.x) return w.x<v.x;
return w.y<v.y;
}
ll det(point a,point b){ return a.x*b.y-a.y*b.x;}
ll dot(point a,point b){ return 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,point b){ return point(a.x-b.x,a.y-b.y);}
point a[maxn],ch[maxn]; int top,ttop;
void convexhull(int N)
{
for(int i=;i<=N;i++){
while(top>&&det(ch[top]-ch[top-],a[i]-ch[top-])<=) top--;
ch[++top]=a[i];
}
ttop=top;
for(int i=N-;i>=;i--){
while(top>ttop&&det(ch[top]-ch[top-],a[i]-ch[top-])<=) top--;
ch[++top]=a[i];
}
}
int get(int L,int R,int i,int w)
{
while(L<R){
int Mid=(L+R)>>;
if(det(ch[Mid]-a[i],ch[Mid+]-a[i])*w>) R=Mid;
else L=Mid+;
}
return L;
}
int bord(int L,int R,int i,int w)
{
while(L<R){
int Mid=(L+R)>>;
if((ch[Mid].x-a[i].x)*w<) L=Mid+;
else R=Mid;
}
return L;
}
ll ans,sum[maxn],tmp;
int main()
{
int N,K;
scanf("%d%d",&N,&K);
rep(i,,N) scanf("%lld%lld",&a[i].x,&a[i].y);
sort(a+,a+K+,cmp); convexhull(K);
rep(i,,top-) ans+=det(ch[i],ch[i+]),sum[i+]=ans;
rep(i,K+,N){
if(a[i].x<ch[].x){
int L=get(,ttop,i,),R=get(ttop,top,i,-);
tmp=sum[R]-sum[L]+det(ch[R],a[i])+det(a[i],ch[L]);
}
else if(a[i].x>ch[ttop].x){
int L=get(,ttop,i,-),R=get(ttop,top,i,);
tmp=sum[top]-sum[R]+sum[L]+det(ch[L],a[i])+det(a[i],ch[R]);
}
else if(det(ch[ttop]-a[],a[i]-ch[])>){//shang
int Mid=bord(ttop,top,i,-);
if(Mid>ttop&&det(ch[Mid]-ch[Mid-],a[i]-ch[Mid-])>) continue;
int L=Mid>ttop?get(ttop,Mid-,i,-):Mid;
int R=get(Mid,top,i,);
tmp=sum[top]-sum[R]+sum[L]+det(ch[L],a[i])+det(a[i],ch[R]);
}
else {
int Mid=bord(,ttop,i,);
if(Mid>&&det(ch[Mid]-ch[Mid-],a[i]-ch[Mid-])>) continue;
int L=Mid>?get(,Mid-,i,-):;
int R=get(Mid,ttop,i,);
tmp=sum[top]-sum[R]+sum[L]+det(ch[L],a[i])+det(a[i],ch[R]);
}
ans=max(ans,tmp);
}
printf("%lld.%lld\n",ans/,ans%*);
return ;
}
/*
5 3
-5 -5
-5 5
5 -5
-4 6
5 5
*/