-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathconvex hull.cpp
55 lines (49 loc) · 1.16 KB
/
convex hull.cpp
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
# include <bits/stdc++.h>
# define NR 120005
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
struct elem {
double x, y;
}a[NR];
bool cmp (elem x, elem y) {
double dx1, dx2, dy1, dy2;
dx1=x.x - a[1].x;
dy1=x.y - a[1].y;
dx2=y.x - a[1].x;
dy2=y.y - a[1].y;
if (dy1*dx2 != dy2*dx1) return dy1*dx2 < dy2*dx1;
else {
if (x.x != y.x) return x.x < y.x;
else return x.y > y.y;
}
}
double det (elem a, elem b, elem c) {
return a.x*b.y + b.x*c.y + c.x*a.y - a.x*c.y - c.x*b.y - b.x*a.y;
}
int i,j,n,m,VV,I;
int st[NR];
void infasuratoare () {
st[1]=1; st[2]=2; VV=2;
for (int i=3; i<=n; ++i) {
while (VV>=2 && det(a[st[VV-1]], a[st[VV]], a[i])<0)
--VV;
st[++VV]=i;
}
}
int main ()
{
f>>n; I=1;
for (i=1; i<=n; ++i) {
f>>a[i].x>>a[i].y;
if (a[i].y<a[I].y) I=i;
else if (a[i].y==a[I].y && a[i].x < a[I].x) I=i;
}
swap(a[1], a[I]);
sort (a+2, a+n+1, cmp);
infasuratoare ();
g<<VV<<"\n";
for (i=1; i<=VV; ++i)
g<<fixed<<setprecision(6)<<a[st[i]].x<<" "<<a[st[i]].y<<"\n";
return 0;
}