Poj--3787(凸包)

2014-12-06 13:17:20

思路:好久没打凸包了,复习下。裸的凸包不多说了,注意下共线和输出顺序即可。

  1 /*************************************************************************
  2     > File Name: 3787.cpp
  3     > Author: Nature
  4     > Mail: 564374850@qq.com 
  5     > Created Time: Sat 06 Dec 2014 11:13:23 AM CST
  6 ************************************************************************/
  7 
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <cstdlib>
 11 #include <cmath>
 12 #include <vector>
 13 #include <map>
 14 #include <set>
 15 #include <stack>
 16 #include <queue>
 17 #include <iostream>
 18 #include <algorithm>
 19 using namespace std;
 20 #define lp (p << 1)
 21 #define rp (p << 1|1)
 22 #define getmid(l,r) (l + (r - l) / 2)
 23 #define MP(a,b) make_pair(a,b)
 24 typedef long long ll;
 25 const int INF = 1 << 30;
 26 const double eps = 1e-8;
 27 
 28 int T,tag,N;
 29 int cnt;
 30 
 31 struct point{
 32     int x,y;
 33 }p[100];
 34 
 35 int cross(point st,point a,point b){
 36     int x1 = a.x - st.x;
 37     int y1 = a.y - st.y;
 38     int x2 = b.x - st.x;
 39     int y2 = b.y - st.y;
 40     return x1 * y2 - x2 * y1;
 41 }
 42 
 43 void Graham(){
 44     cnt = 2; //the number of vertices    
 45     for(int i = 3; i <= N; ++i){
 46         while(cnt >= 2 && cross(p[cnt - 1],p[cnt],p[i]) >= 0){
 47             cnt--;
 48         }
 49         p[++cnt] = p[i];
 50     }
 51 }
 52 
 53 bool cmpx(point a,point b){
 54     if(a.x == b.x)
 55         return a.y < b.y;
 56     return a.x < b.x;
 57 }
 58 
 59 bool cmpy(point a,point b){
 60     if(a.y == b.y)
 61         return a.x < b.x;
 62     return a.y > b.y;
 63 }
 64 
 65 bool cmp(point a,point b){
 66     int k = cross(p[1],a,b);
 67     if(k == 0){
 68         return cmpx(a,b);
 69     }
 70     return k < 0;
 71 }
 72 
 73 int main(){
 74     scanf("%d",&T);
 75     while(T--){
 76         scanf("%d%d",&tag,&N);
 77         int st = -1;
 78         for(int i = 1; i <= N; ++i){
 79             scanf("%d%d",&p[i].x,&p[i].y);
 80             if(st == -1 || cmpx(p[i],p[st])){
 81                 st = i;
 82             }
 83         }
 84         swap(p[1],p[st]);
 85         sort(p + 2,p + N + 1,cmp);
 86         Graham();
 87         printf("%d %d\n",tag,cnt);
 88         st = -1;
 89         for(int i = 1; i <= cnt; ++i){
 90             if(st == -1 || cmpy(p[i],p[st])){
 91                 st = i;
 92             }
 93         }
 94         swap(p[1],p[st]);
 95         sort(p + 2,p + cnt + 1,cmp);
 96         for(int i = 1; i <= cnt; ++i){
 97             printf("%d %d\n",p[i].x,p[i].y);
 98         }
 99     }
100     return 0;
101 }

 

posted @ 2014-12-06 13:18  Naturain  阅读(205)  评论(0编辑  收藏  举报