Pagini recente »
Borderou de evaluare (job #811277)
|
Borderou de evaluare (job #257415)
|
Tema 6 Clasa a-7-a
|
Borderou de evaluare (job #233417)
|
Cod sursă (job #644193)
Cod sursă (job
#644193)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
struct Point
{
int x, y;
};
Point p0;
Point nextToTop(stack<Point> &st)
{
Point p = st.top();
st.pop();
Point res = st.top();
st.push(p);
return res;
}
void swap(Point &p1, Point &p2)
{
Point temp = p1;
p1 = p2;
p2 = temp;
}
int distSq(Point p1, Point p2)
{
return (p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y);
}
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
int compare(const void *vp1, const void *vp2)
{
Point *p1 = (Point *)vp1;
Point *p2 = (Point *)vp2;
int o = orientation(p0, *p1, *p2);
if (o == 0)
return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;
return (o == 2)? -1: 1;
}
void convexHull(vector<Point> &points, int n)
{
int ymin = points[0].y, min = 0;
for (int i = 1; i < n; i++)
{
int y = points[i].y;
if ((y < ymin) || (ymin == y &&
points[i].x < points[min].x))
ymin = points[i].y, min = i;
}
swap(points[0], points[min]);
p0 = points[0];
qsort(&points[1], n-1, sizeof(Point), compare);
int m = 1;
for (int i=1; i<n; i++)
{
while (i < n-1 && orientation(p0, points[i],
points[i+1]) == 0)
i++;
points[m] = points[i];
m++;
}
if (m < 3) return;
stack<Point> st;
st.push(points[0]);
st.push(points[1]);
st.push(points[2]);
for (int i = 3; i < m; i++)
{
while (st.size()>1 && orientation(nextToTop(st),st.top(), points[i]) != 2)
st.pop();
st.push(points[i]);
}
stack<Point> st2;
while (!st.empty())
{
st2.push(st.top());
st.pop();
}
fout<<st2.size()<<'\n';
while(!st2.empty()){
fout<<st2.top().x<<' '<<st2.top().y<<'\n';
st2.pop();
}
}
int main()
{
int n;
fin>>n;
std::vector<Point> v;
int a,b;
for(int i=1;i<=n;i++){
fin>>a>>b;
v.push_back({a,b});
}
convexHull(v, n);
return 0;
}