Cod sursa(job #2280370)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 10 noiembrie 2018 15:16:27
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <ios>
#include <iomanip>

#include <cmath>
#include <fstream>
#include <algorithm>

#define NMAX 120010

using std::sort;
using std::fixed;
using std::setprecision;

std::ifstream fin("infasuratoare.in");
std::ofstream fout("infasuratoare.out");

struct Point {
    double x, y;
};

int n;
Point p[NMAX];

int min, vf;
Point st[NMAX];

bool operator<(Point a, Point b) {
    double x = atan2(a.y - p[0].y, a.x - p[0].x);
    double y = atan2(b.y - p[0].y, b.x - p[0].x);

    double r1 = (a.x - p[0].x) * (a.x - p[0].x) + (a.y - p[0].y) * (a.y - p[0].y);
    double r2 = (b.x - p[0].x) * (b.x - p[0].x) + (b.y - p[0].y) * (b.y - p[0].y);

    return x < y || x == y && r1 < r2;
}

inline void swap(Point& a, Point& b) {
    Point aux = a;
    a = b;
    b = aux;
}

double position(Point a, Point b, Point c) {
    return (a.x - b.x) * (c.y - b.y) - (c.x - b.x) * (a.y - b.y);
}

int main() {
    fin >> n;
    for (int i = 0; i < n; i++)
        fin >> p[i].x >> p[i].y;

    for (int i = 1; i < n; i++)
        if (p[i].x < p[min].x || p[i].x == p[min].x && p[i].y < p[min].y)
            min = i;

    swap(p[0], p[min]);
    sort(p + 1, p + n);

    vf = 2;
    st[1] = p[0];
    st[2] = p[1];

    for (int i = 2; i < n; i++) {
        while (vf > 2 && position(st[vf - 1], st[vf], p[i]) >= 0)
            vf--;
        st[++vf] = p[i];
    }

    fout << fixed;
    fout << setprecision(6);

    fout << vf << '\n';
    for (int i = 1; i <= vf; i++)
        fout << st[i].x << ' ' << st[i].y << '\n';

    fout.close();
    return 0;
}