2 条题解

  • 1
    @ 2026-1-28 23:12:20
    #include<bits/stdc++.h>
    using namespace std;
    const int N=5e5+10;
    struct Point
    {
        double x,y;
        Point(){}
        Point(double a,double b):x(a),y(b){}
        void input(){scanf("%lf%lf",&x,&y);} 
        friend bool operator<(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
        friend bool operator==(Point a,Point b){return a.x==b.x && a.y==b.y;}
        friend Point operator-(Point a,Point b){ return {a.x-b.x,a.y-b.y};}
        friend double det(Point a,Point b){return a.x*b.y-a.y*b.x;}
        friend double dis(Point a,Point b){Point p=a-b;return sqrt(p.x*p.x+p.y*p.y);}
    };
    Point P[N],sta[N];int n,top;
    
    void Andrew()
    {
        sort(P+1,P+n+1);
        top=0;
        for(int i=1;i<=n;i++)
        {
            while( top>1 && det(sta[top]-sta[top-1],P[i]-sta[top-1])<=0 ) top--;
            sta[++top]=P[i];
        }
        int t=top;
        for(int i=n-1;i>=1;i--)
        {
            while( top>t && det(sta[top]-sta[top-1],P[i]-sta[top-1])<=0 ) top--;
            sta[++top]=P[i];
        }
        n=top-1;//注意:sta[1]和sta[top]都是P[1]  
        while(n>1 && sta[n]==sta[n-1]) n--;
    }
    int main()
    {
        int T;scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)P[i].input();
            if(n==0) {printf("0\n");continue;}
            if(n==1) {printf("1\n%0.0lf %0.0lf\n",P[1].x,P[1].y);continue;} 
            Andrew();
            printf("%d\n",n);  
            for(int i=1;i<=n;i++) printf("%0.0lf %0.0lf\n",sta[i].x,sta[i].y);
        }
        return 0;
    }
    
    
    • 0
      @ 2026-8-15 9:50:31

      • 1

      *【计算几何】静态凸包Static Convex Hull

      信息

      ID
      3328
      时间
      300ms
      内存
      1024MiB
      难度
      10
      标签
      递交数
      3
      已通过
      2
      上传者