Cod sursă (job #301420)

Utilizator avatar nurof3n Cioc Alex Andrei nurof3n IP ascuns
Problemă Înfășurătoare convexă Compilator cpp | 1,47 kb
Rundă Arhiva de probleme Status evaluat
Dată 14 apr. 2017 12:21:59 Scor 20
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
struct coord
{
    long long x;
    long long y;
} p[100005], st[100005];
int N, vf, lg;
inline long long crossproduct(coord a, coord b, coord c)
{
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
inline bool cmp(coord a, coord b)
{
    return crossproduct(p[1], a, b) < 0;
}
void citire()
{
    f >> N;
    int poz = 1;
    for(int i = 1; i <= N; i++)
    {
        f >> p[i].x >> p[i].y;
        if(p[i].x < p[poz].x) poz = i;
        else if(p[i].x == p[poz].x && p[i].y < p[poz].y) poz = i;
    }
    swap(p[1], p[poz]);
}
void afisare()
{
    g << lg << '\n';
    for(int i = vf; i >= 1; i--)
        if(st[i].x != 1 << 31)
            g << st[i].x << ' ' << st[i].y << '\n';
}
void graham()
{
    sort(p + 2, p + N + 1, cmp);
    st[++vf] = p[1], st[++vf] = p[2];
    for(int i = 3; i <= N; i++)
    {
        while(vf >= 2 && crossproduct(st[vf - 1], st[vf], p[i]) > 0) vf--; //pct constituie o intoarcere la dreapta
        st[++vf] = p[i];
    }
    //verificam daca avem pct coliniare
    lg = vf;
    st[0] = st[vf], st[vf + 1] = st[1];
    for(int i = vf; i >= 1; i--)
        if(crossproduct(st[i + 1], st[i], st[i - 1]) == 0) st[i].x = 1 << 31, lg--;
}
int main()
{
    citire();
    graham();
    afisare();
    return 0;
}