Cod sursa(job #2200941)

Utilizator LivcristiTerebes Liviu Livcristi Data 2 mai 2018 21:50:35
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#define NUM 120005
using namespace std;
struct Punct
{
    double x, y;
};
Punct v[NUM];
Punct stiv[NUM];
int n, varf;
double cross_product(Punct A, Punct B, Punct C)
{
    return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);
}
int cmp(Punct A, Punct B)
{
    return cross_product(v[1], A, B) < 0;
}
void sortare()
{
    int poz = 1;

    for(int i = 2; i <= n; ++i)
    {
        if(v[i].x < v[poz].x || (v[i].x == v[poz].x && v[i].y < v[poz].y))
            poz = i;
    }
    swap(v[poz], v[1]);
    sort(v + 2, v + n + 1, cmp);
}
void infasur()
{
    sortare();

    stiv[1] = v[1];
    stiv[2] = v[2];
    varf = 2;
    for(int i = 3; i <= n; ++i)
    {
        while (varf >= 2 && cross_product(stiv[varf - 1], stiv[varf], v[i]) > 0)
            varf--;
        stiv[++varf] = v[i];
    }
}
int main()
{
    ifstream f("infasuratoare.in");
    ofstream g("infasuratoare.out");
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> v[i].x >> v[i].y;
    infasur();
    g << varf << "\n";
    for(int i = varf; i >= 1; --i)
        g << fixed << setprecision(9) << stiv[i].x << " " << stiv[i].y << "\n";
    f.close();
    g.close();
}