标签:
1 4 0 0 1 1 2 3 3 0
0 0 2 3 3 0
题解:用到了差积的特性,如果差积为负就是顺时针转,如果为正就是逆时针,如果为0就是共线;
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 struct Node{ 8 int x,y; 9 friend int operator < (Node a,Node b){ 10 if(a.x<b.x||(a.x==b.x&&a.y<b.y))return 1; 11 else return 0; 12 } 13 }; 14 int chaji(Node a,Node b,Node c){ 15 return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); 16 } 17 Node a[110],ans[110]; 18 int main(){ 19 int N,m; 20 scanf("%d",&N); 21 while(N--){ 22 scanf("%d",&m); 23 for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y); 24 sort(a,a+m); 25 int k=0; 26 for(int i=0;i<m;i++){ 27 while(k>1&&chaji(ans[k-2],ans[k-1],a[i])<=0)k--; 28 ans[k++]=a[i]; 29 } 30 int t=k; 31 for(int i=m-1;i>=0;i--){ 32 while(k>t&&chaji(ans[k-2],ans[k-1],a[i])<=0)k--; 33 ans[k++]=a[i]; 34 } 35 k--; 36 sort(ans,ans+k); 37 for(int i=0;i<k;i++)printf("%d %d\n",ans[i].x,ans[i].y); 38 } 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4929971.html