Cod sursa(job #2275867)

Utilizator Andreea_DanielaAndreea Guster Andreea_Daniela Data 3 noiembrie 2018 18:27:19
Problema Infasuratoare convexa Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iomanip>
#define x first
#define y second
#define MAX_N 120005

using namespace std;

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

typedef pair<double, double> Point;
Point hull[MAX_N], nonhull[MAX_N], v[MAX_N];;
int head, n, nr=1;;

inline double cross_product(const Point& A, const Point& B, const Point& C) { //determinantul
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

inline int cmp(const Point& p1, const Point& p2) {
    return cross_product(v[1], p1, p2) < 0;
}

void sort_points() {
    int pos = 1;
    for (int i = 2; i <= n; ++i)
        if (v[i] < v[pos])
            pos = i;
    swap(v[1], v[pos]);
    sort(v + 2, v + n + 1, cmp);
}

void convex_hull() {

    sort_points();

    hull[1] = v[1];
    hull[2] = v[2];
    head = 2;
    for (int i = 3; i <= n; ++i) {
        while (head >= 2 && cross_product(hull[head - 1], hull[head], v[i]) > 0)
            --head;
        hull[++head] = v[i];
    }
    for(int i=1; i<=n; i++)
    {   int ok=0;
       for(int j=1; j<= head; j++)
          if(hull[j] == v[i])
           ok=1;
        if(ok==0)
            nonhull[nr++]= v[i];
    }
}

int main() {

    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> v[i].x >> v[i].y;

    convex_hull();

    fout << fixed << head << "\n";
    for (int i = head; i >= 1; --i)
    {
      fout << setprecision(9) << hull[i].x << " " << hull[i].y << "\n";
    }
    fout << endl;
}