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 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/4148151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值