Cod sursa(job #1970586)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 19 aprilie 2017 14:23:38
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
//Convex Hull
//Graham Scan
#include <bits/stdc++.h>
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
int N;

struct Point
{
    double x;
    double y;

} V[120001],Stack[120001];
int head;

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


inline bool 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].x < V[pos].x)
            pos = i;
    swap(V[1],V[pos]);
    sort(V+2,V+N+1, cmp);
}

void convex_hull()
{

    Stack[1] = V[1];
    Stack[2] = V[2];
    head = 2;
    for (int i = 3; i <= N; ++i)
    {
        while (head >= 2 && cross_product(Stack[head - 1], Stack[head], V[i]) > 0)  --head;
        Stack[++head] = V[i];
    }
}

int main()
{
    f>>N;
    for(int i=1; i<=N; i++)
    {
        double x,y;
        f>>x>>y;
        V[i]={x,y};
    }
    sort_points();
    convex_hull();
    g<< fixed;
    g<< head << "\n";
    for (int i = head; i >= 1; --i)
    g << setprecision(9) << Stack[i].x << " " << Stack[i].y << "\n";
}