Cod sursa(job #1607618)

Utilizator Burbon13Burbon13 Burbon13 Data 21 februarie 2016 14:14:44
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <cstdio>
#include <iostream>
#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 <int> 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 <int,int> a, pair <int,int> b, pair <int,int> c){
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

struct Cmp{
    bool operator() (pair <int,int> a, pair <int,int> b) {
        return determinant(v[1],a,b) > 0;
    }
} Cmp;

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

    citire();

    int pmin = 1;
    for(int i = 2; i <= n; ++i)
        if(v[i] < v[pmin])
            pmin = i;

    swap(v[1],v[pmin]);

    sort(v+2,v+n+1,Cmp);

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

    printf("%d\n", st.size());
    for(vector<int>::iterator it = st.begin(); it != st.end(); ++it)
        printf("%lf %lf\n", v[*it].x, v[*it].y);

    return 0;
}