Cod sursa(job #1883031)

Utilizator Burbon13Burbon13 Burbon13 Data 17 februarie 2017 17:53:07
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <cstdio>
#include <algorithm>
#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;

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;

int main()
{
    freopen("infasuratoare.in", "r", stdin);
    freopen("infasuratoare.out", "w", stdout);

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

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

    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]);
    }

    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);

    return 0;
}