Cod sursa(job #1368962)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 2 martie 2015 20:47:11
Problema Infasuratoare convexa Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int n, nq;

struct punct
{
    double x, y;
    punct(double x=0, double y=0) : x(x), y(y)
    {

    }
    bool operator()(punct a, punct b)
    {
        return (a.x == b.x ? a.y - b.y < 0.0001 : a.x - b.x < 0.0001);
    }

}a[120012];

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

int determinant(punct f, punct g, punct h)
{
    return (f.x * g.y + g.x * h.y + h.x * f.y) - (h.x * g.y + g.x * f.y + f.x * h.y);
}

double cross_product(punct O, punct A, punct B) {
    return (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y);
}

void operate(vector<punct> &st, punct g)
{
    while(st.size() >= 2 && cross_product(st[st.size()-1], st[st.size()-2], g) > 0.0001)//determinant(st[st.size()-1], st[st.size()-2], g) > 0.0001)
        st.pop_back();
    st.push_back(g);
}

void solve()
{
    sort(a, a+n, punct());
    vector<punct> st;
    st.push_back(a[0]); st.push_back(a[1]);
    for (int i = 2; i < n; i++)
        operate(st, a[i]);
    vector<punct> st2;
    st2.push_back(a[n-1]); st2.push_back(a[n-2]);
    for (int i = n-3; i >= 0; i--)
        operate(st2, a[i]);

    printf("%d\n", st.size() + st2.size()-2);
    for (int i = 0; i < st.size()-1; i++)
        printf("%lf %lf\n", st[i]);
    for (int j = 0; j < st2.size()-1; j++)
        printf("%lf %lf\n", st2[j]);
}

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

    citire();
    solve();

    return 0;
}