Cod sursa(job #1970592)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 19 aprilie 2017 14:32:19
Problema Infasuratoare convexa Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.91 kb
//Convex Hull
//Graham Scan
#include <bits/stdc++.h>
#define verf ++poz == (1<<17) ? fread ( ch, 1, (1<<17), stdin ), poz = 0 : 0
using namespace std;

ofstream g("infasuratoare.out");
int N,poz, ff[] = {1, 10, 100, 1000, 10000, 100000, 1000000};;
char ch[1<<17];

inline void cit (double &x)
{
    int semn = 1, vir = -1;
    if ( ch[0] == '\0' ) fread ( ch, 1, 1<<17, stdin ) ;
    else for ( ; (ch[poz] < '0' || ch[poz] > '9') && ch[poz] != '-' && ch[poz] != '.'; verf ) ;
    for ( x = 0 ; ch[poz] >= '0' && ch[poz] <= '9' || ch[poz] == '-' || ch[poz] == '.'; verf ) {
        if (ch[poz] == '-') semn = -1;
        else if (ch[poz] == '.') vir = 0;
        else if (vir == -1) x = x * 10 + ch[poz] - '0';
        else x += 1.0 * (ch[poz] - '0') / ff[++vir] ;
    }
    x *= semn;
}

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()
{
    freopen ("infasuratoare.in", "r", stdin);


    scanf ("%d\n", &N);
    for (int i = 1; i <= N; ++i)
        cit (V[i].x), cit (V[i].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";
}