给一个凸包,要求逆时针排序,刚开始一直因为极角排序就是逆时针的,所以一直wa,后来发现极角排序距离相同是,排的是随机的,所以要对末尾角度相同的点重新排一次
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-12; const int N=100000+10,maxn=500+100,inf=0x3f3f3f; struct point{ double x,y; }; point p[N],s[N]; int n; inline bool zero(double x) { return fabs(x)<eps; } double dir(point p1,point p2,point p3) { return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x); } double dis(point p1,point p2) { return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); } bool comp(point p1,point p2) { double te=dir(p[0],p1,p2); if(te<0)return 1; if(zero(te)&&dis(p[0],p1)<dis(p[0],p2))return 1; return 0; } bool comp1(point p1,point p2) { return dis(p[0],p1)>dis(p[0],p2); } void Graham() { int pos; double minx=inf,miny=inf; for(int i=0;i<n;i++) { if(p[i].x<minx||(p[i].x<minx&&p[i].y<miny)) { minx=p[i].x; miny=p[i].y; pos=i; } } swap(p[0],p[pos]); sort(p+1,p+n,comp); int k=n-1; while(1){ if(k>=1&&zero(dir(p[0],p[k-1],p[k])))k--; else break; } sort(p+k,p+n,comp1); cout<<n<<endl; for(int i=0;i<n;i++) cout<<p[i].x<<" "<<p[i].y<<endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout<<setiosflags(ios::fixed)<<setprecision(0); int t; cin>>t; while(t--){ cin>>n; string c; for(int i=0;i<n;) { cin>>p[i].x>>p[i].y>>c; if(c[0]=='Y')i++; else n--; } Graham(); } return 0; } /********************* 1 5 1 1 Y 1 -1 Y 0 0 N -1 -1 Y -1 1 Y 3 12 0 0 Y 2 0 Y 1 0 Y 3 0 Y 3 3 Y 1 3 Y 2 3 Y 3 1 Y 3 2 Y 0 1 Y 0 2 Y 0 3 Y 3 0 0 Y 1000000000 1000000000 Y 1000000000 0 Y 8 0 -2 Y 1 -1 Y 2 0 Y 1 1 Y 0 2 Y -1 1 Y -2 0 Y -1 -1 Y *********************/