Pagini recente » Monitorul de evaluare | Cod sursa (job #927334) | Borderou de evaluare (job #1322625) | Cod sursa (job #3358138) | Cod sursa (job #3357981)
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Point {
double x, y;
bool operator<(const Point& p) const {
return x < p.x || (x == p.x && y < p.y);
}
bool operator==(const Point& p) const {
return x == p.x && y == p.y;
}
};
double cross(const Point& O, const Point& A, const Point& B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
vector<Point> convex_hull(vector<Point> points) {
int n = points.size(), k = 0;
vector<Point> hull(2 * n);
sort(points.begin(), points.end());
for (int i = 0; i < n; ++i) {
while (k >= 2 && cross(hull[k-2], hull[k-1], points[i]) <= 0) k--;
hull[k++] = points[i];
}
for (int i = n-2, t = k+1; i >= 0; --i) {
while (k >= t && cross(hull[k-2], hull[k-1], points[i]) <= 0) k--;
hull[k++] = points[i];
}
hull.resize(k-1);
return hull;
}
int main() {
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
fout << fixed << setprecision(6);
int n;
fin >> n;
vector<Point> points(n);
for (int i = 0; i < n; ++i) {
fin >> points[i].x >> points[i].y;
}
vector<Point> hull = convex_hull(points);
fout << hull.size() << '\n';
for (const auto& p : hull) {
fout << p.x << ' ' << p.y << '\n';
}
fin.close();
fout.close();
return 0;
}