Cod sursa(job #2857868)

Utilizator ptudortudor P ptudor Data 26 februarie 2022 15:17:45
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.26 kb
#include <bits/stdc++.h>
#define double long double
using namespace std;

#ifdef LOCAL
ifstream in("in.in");
ofstream out("out.out");
#else
ifstream in("infasuratoare.in");
ofstream out("infasuratoare.out");
#endif

struct Point {
    double x,y;

    bool operator < (const Point other) const {
        if (y != other.y) {
            return y < other.y;
        }
        return x < other.x;
    }

    Point operator - (const Point other) const {
        return Point{x - other.x , y - other.y};
    }
};

double angle(Point p) { /// returns in hexadecimal degrees the angle of the Point
    if (p.x == 0) {
        if (p.y == 0) return 0;
        if (p.y > 0) return M_PI/2;
        if (p.y < 0) return M_PI + M_PI / 2;
    }
    if (p.x > 0 && p.y > 0) {
        return atan(p.y / p.x);
    } else if ( p.x > 0 && p.y < 0) {
        return atan(p.y / p.x) + 2 * M_PI; /// pi = 180 degrees
    } else {
        return atan(p.y / p.x) + M_PI;
    }
}

bool rightTurn(Point a, Point b, Point c) {
    return angle(b - a) > angle(c - b);
}
vector<Point> pt;
int n,i;
double x,y;
vector<Point> res;
Point lowLeft;

vector<Point> convexHull() {

    lowLeft = pt[1];
    for (i = 2; i <= n; i++) {
        if (pt[i] < lowLeft) {
            lowLeft = pt[i];
        }
    }
    sort(pt.begin() + 1, pt.end() , [&](Point a, Point b) {
        ///we move everyone down by this low left guy, then sort this increasing by it's angle.
        ///this is also cool cause this makes the origin(lowLeft the first one)
        return angle(a - lowLeft) < angle(b - lowLeft);
    });

    for (i = 1; i <= n; i++) {
        while(res.size() >= 2 && rightTurn(res[(int)res.size() - 2] , res[(int)res.size() - 1] , pt[i])) {
            res.pop_back();
        }
        res.push_back(pt[i]);
    }
    return res;
}

int main() {
    string N;
    in >> N;
    n = stold(N);
    pt.resize(n + 1);
    for (i = 1; i <= n; i++) {
        string X,Y;
        in >> X >> Y;
        x = stold(X);
        y = stold(Y);
        pt[i] = {x,y};
    }


    convexHull();

    out << fixed << setprecision(12);
    out << res.size() << "\n";
    for (auto k : res) {
        out << k.x << " " << k.y << "\n";
    }
    out << "\n";
}