凸包性质——cf1044C
由题目给出的条件不难发现这是个凸包
又因为要求的是曼哈顿距离的最大值,所以当k>=4时都是同一个答案,即上下左右的最远点构成的凸包外接矩形
当k=3时固定两个极点,枚举第三个点即可(赋值粘贴就完事了)
#include<bits/stdc++.h> using namespace std; #define N 300005 #define ll long long struct point{ ll x,y; }p[N],l,r,u,d; int n; int cmp1(point &a,point &b){return a.x<b.x;} int cmp2(point &a,point &b){return a.x>b.x;} int cmp3(point &a,point &b){return a.y>b.y;} int cmp4(point &a,point &b){return a.y<b.y;} int equ(point a,point b){return a.x==b.x && a.y==b.y;} int main(){ cin>>n; for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y; sort(p+1,p+1+n,cmp1);l=p[1]; sort(p+1,p+1+n,cmp2);r=p[1]; sort(p+1,p+1+n,cmp3);u=p[1]; sort(p+1,p+1+n,cmp4);d=p[1]; ll Max=0; for(int i=1;i<=n;i++){ if(equ(p[i],l)||equ(p[i],r))continue; point k1=p[i],k2=l,k3=r; ll up=max(k1.y,max(k2.y,k3.y)); ll down=min(k1.y,min(k2.y,k3.y)); ll ri=max(k1.x,max(k2.x,k3.x)); ll le=min(k1.x,min(k2.x,k3.x)); Max=max(Max,2*(ri-le)+2*(up-down)); } for(int i=1;i<=n;i++){ if(equ(p[i],u)||equ(p[i],d))continue; point k1=p[i],k2=u,k3=d; ll up=max(k1.y,max(k2.y,k3.y)); ll down=min(k1.y,min(k2.y,k3.y)); ll ri=max(k1.x,max(k2.x,k3.x)); ll le=min(k1.x,min(k2.x,k3.x)); Max=max(Max,2*(ri-le)+2*(up-down)); } for(int i=1;i<=n;i++){ if(equ(p[i],l)||equ(p[i],d))continue; point k1=p[i],k2=l,k3=d; ll up=max(k1.y,max(k2.y,k3.y)); ll down=min(k1.y,min(k2.y,k3.y)); ll ri=max(k1.x,max(k2.x,k3.x)); ll le=min(k1.x,min(k2.x,k3.x)); Max=max(Max,2*(ri-le)+2*(up-down)); } for(int i=1;i<=n;i++){ if(equ(p[i],l)||equ(p[i],u))continue; point k1=p[i],k2=l,k3=u; ll up=max(k1.y,max(k2.y,k3.y)); ll down=min(k1.y,min(k2.y,k3.y)); ll ri=max(k1.x,max(k2.x,k3.x)); ll le=min(k1.x,min(k2.x,k3.x)); Max=max(Max,2*(ri-le)+2*(up-down)); } for(int i=1;i<=n;i++){ if(equ(p[i],r)||equ(p[i],d))continue; point k1=p[i],k2=r,k3=d; ll up=max(k1.y,max(k2.y,k3.y)); ll down=min(k1.y,min(k2.y,k3.y)); ll ri=max(k1.x,max(k2.x,k3.x)); ll le=min(k1.x,min(k2.x,k3.x)); Max=max(Max,2*(ri-le)+2*(up-down)); } for(int i=1;i<=n;i++){ if(equ(p[i],r)||equ(p[i],u))continue; point k1=p[i],k2=r,k3=u; ll up=max(k1.y,max(k2.y,k3.y)); ll down=min(k1.y,min(k2.y,k3.y)); ll ri=max(k1.x,max(k2.x,k3.x)); ll le=min(k1.x,min(k2.x,k3.x)); Max=max(Max,2*(ri-le)+2*(up-down)); } cout<<Max<<" "; for(int i=4;i<=n;i++) cout<<2*(u.y-d.y)+2*(r.x-l.x)<<" "; }