凸包学习了
#include <iostream> #include <algorithm> #include <stdio.h> #include <queue> #include <limits.h> #include <string.h> #include <vector> #include <map> #include <math.h> #define LL long long #define INF 2100000000 #define fi first #define se second #define lowbit(x) (x&(-x)) #define eps 5e-7 using namespace std; const int maxn=(int)1e4 +30; const int MOD=998244353; struct point { LL x,y; point() {} point(LL x,LL y):x(x),y(y) {} point operator -(const point& a)const { return point(x-a.x,y-a.y); } LL operator *(const point& a)const { return (x*a.y)-(y*a.x); } bool operator <(const point &a)const { if(x!=a.x)return x<a.x; return y<a.y; } void input() { scanf("%I64d%I64d",&x,&y); } void output() { printf("%I64d %I64d ",x,y); } }; point p[maxn]; point cp[maxn]; int n; int Andrew() { sort(p,p+n); int m=0; for(int i=0; i<n; i++) // bottom half { while(m>1&&(cp[m-1]-cp[m-2])*(p[i]-cp[m-2])<=0) m--; // cout<<"i: "<<i<<" "<<"m: "<<m<<" "<<(cp[m-1]-cp[m-2])*(p[i]-cp[m-2])<<endl; cp[m++]=p[i]; } int k=m; for(int i=n-2; i>=0; i--) // top half { while(m>k&&(cp[m-1]-cp[m-2])*(p[i]-cp[m-2])<=0)m--; // cout<<"i: "<<i<<" "<<"m: "<<m<<" "<<(cp[m-1]-cp[m-2])*(p[i]-cp[m-2])<<endl; cp[m++]=p[i]; } if(n>1) m--;// p[0] will be insert again return m; } int main() { #ifdef shuaishuai freopen("C:\Users\hasee\Desktop\a.txt","r",stdin); // freopen("C:\Users\hasee\Desktop\b.txt","w",stdout); #endif while(scanf("%d",&n)!=EOF,n!=0 ) { for(int i=0; i<n; i++)p[i].input(); LL a=0,b=0; int m=Andrew(); for(int i=0; i<m; i++) { LL dy=abs(cp[i].y-cp[(i+1)%m].y) ; LL dx=abs(cp[i].x-cp[(i+1)%m].x ); b+=min(dy,dx); a+=abs(dy-dx); } cout<<a<<" "<<b<<endl; // printf("%I64d %I64d ",a,b); } return 0; }