Cod sursa(job #1889958)

Utilizator Burbon13Burbon13 Burbon13 Data 22 februarie 2017 22:35:16
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#define x first
#define y second

using namespace std;

const int nmx = 120002;

int n;
pair <double,double> v[nmx];
vector <pair<double,double> > st;

void citire()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%lf %lf", &v[i].x, &v[i].y);
}

double determinant(pair <double,double> p1, pair <double,double> p2, pair <double,double> p3)
{
    return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
}

struct Cmp
{
    bool operator() (pair <double,double> p1, pair <double,double> p2)
    {
        return determinant(v[1],p1,p2) > 0;
    }
} cmp;

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

void infasuratoare()
{
    for(int i = 1; i <= n; ++i)
    {
        while(st.size() >= 2 && determinant(st[st.size()-2],st[st.size()-1],v[i]) < 0)
            st.pop_back();
        st.push_back(v[i]);
    }
}

void afish()
{
    printf("%d\n", st.size());
    for(vector<pair<double,double> >::iterator it = st.begin(); it != st.end(); ++it)
        printf("%.6lf %.6lf\n", it->x, it->y);

}

int main()
{
    freopen("infasuratoare.in", "r", stdin);
    freopen("infasuratoare.out", "w", stdout);
    citire();
    set_min();
    infasuratoare();
    afish();
    return 0;
}