-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvex-Hull.cpp
More file actions
70 lines (70 loc) · 1.53 KB
/
Convex-Hull.cpp
File metadata and controls
70 lines (70 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
TASK:
LANG: CPP
AUTHOR: PeaTT~
SCHOOL: RYW
*/
#include<bits/stdc++.h>
using namespace std;
struct point{
long long x,y;
};
point convex[50005];
long long dist(point p1,point p2){
return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int orientation(point p0,point p1,point p2){
long long res=(p1.y-p0.y)*(p2.x-p0.x)-(p2.y-p0.y)*(p1.x-p0.x);
if(res<0) return 0;//left turn
if(res>0) return 1;//right turn
if(res==0) return 2;//int the same line
}
bool cmp(point p1,point p2){
int res=orientation(convex[1],p1,p2);
if(res==0) return true;
if(res==1) return false;
if(res==2) return dist(convex[1],p1)<dist(convex[1],p2);
}
point sta[50005];
int top;
int main()
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%lld %lld",&convex[i].x,&convex[i].y);
}
for(i=2;i<=n;i++){
if(convex[1].x>convex[i].x){
swap(convex[1],convex[i]);
}
else if(convex[1].x==convex[i].x){
if(convex[1].y<convex[i].y){
swap(convex[1],convex[i]);
}
}
}
sort(convex+2,convex+n+1,cmp);
sta[++top] = convex[1];
sta[++top] = convex[2];
for(i=3;i<=n;i++){
while(top>1&&orientation(sta[top-1],sta[top],convex[i])!=0){
top--;
}
sta[++top]=convex[i];
}
while(orientation(sta[top-1],sta[top],convex[1])==2){
top--;
}
printf("%d\n",top);
for(i=1;i<=top;i++) printf("%lld %lld\n",sta[i].x,sta[i].y);
return 0;
}
/*
5
0 0
0 1
1 0
0 -1
-1 0
*/