Cod sursa(job #3357949)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 22:08:30
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) : x(x), y(y) {}
};

double cross(Point A, Point B, Point C) {
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

int main() {
    ifstream in("infasuratoare.in");
    ofstream out("infasuratoare.out");

    int n;
    in >> n;
    vector<Point> pts(n);
    Point ref(INT32_MAX, INT32_MAX);
    int pos = 0;
    for (int i = 0; i < n; ++i) {
        in >> pts[i].x >> pts[i].y;
        if (pts[i].x < ref.x || (pts[i].x == ref.x && pts[i].y < ref.y)) {
            ref = pts[i];
            pos = i;
        }
    }
    swap(pts[pos], pts[0]);

    sort(pts.begin() + 1, pts.end(), [ref](const Point& a, const Point& b) {
        double cr = cross(ref, a, b);
        if (cr != 0) return cr < 0;
        double d1 = (ref.x - a.x) * (ref.x - a.x) + (ref.y - a.y) * (ref.y - a.y);
        double d2 = (ref.x - b.x) * (ref.x - b.x) + (ref.y - b.y) * (ref.y - b.y);
        return d1 < d2;
    });

    vector<Point> hull;
    hull.push_back(pts[0]);
    for (int i = 1; i < n; ++i) {
        while (hull.size() >= 2 && cross(hull[hull.size() - 2], hull[hull.size() - 1], pts[i]) > 0) {
            hull.pop_back();
        }
        hull.push_back(pts[i]);
    }

    out << hull.size() << "\n";
    out << setprecision(12) << fixed;
    for (int i = 0; i < hull.size(); ++i) {
        out << hull[i].x << " " << hull[i].y << "\n";
    }

    return 0;
}