Cod sursa(job #2820380)

Utilizator DauCuDalta43Diaconu Razvan DauCuDalta43 Data 20 decembrie 2021 12:00:53
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <bits/stdc++.h>
#define nmax 120003
using namespace std;

struct Punct
{
    double x, y;
};

int st[nmax];
Punct a[nmax];
int n, top, v[nmax], k;

void Citire()
{
    int i;
    ifstream fin("infasuratoare.in");
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> a[i].x >> a[i].y;
    fin.close();
}

inline bool Compara(Punct A, Punct B)
{
    if (A.y == B.y) return A.x < B.x;
    return A.y < B.y;
}

double F(int i, int j, Punct W)
{
    return (a[i].y - a[j].y) * W.x + (a[j].x - a[i].x) * W.y +
        a[i].x * a[j].y - a[j].x * a[i].y;
}

void Hill()
{
    int i;
    Punct P;
    sort(a + 1, a + n + 1, Compara);
    st[++top] = 1;
    st[++top] = 2;
    v[2] = 1;
    for (i = 3; i <= n; i++)
    {
        P = a[i];
        while (top >= 2 && F(st[top-1], st[top], P) < 0)
        {
            v[st[top]] = 0;
            top--;
        }
        st[++top] = i;
        v[i] = 1;
    }
    /// merg inapoi pe punctele nevizitate inca:
    for (i = n - 1; i >= 1; i--)
        if (v[i] == 0)
        {
            P = a[i];
            while (F(st[top-1], st[top], P) < 0)
            {
                v[st[top]] = 0;
                top--;
            }
            st[++top] = i;
            v[i] = 1;
        }

    ofstream fout("infasuratoare.out");
    fout << top - 1 << "\n";
    fout << setprecision(12) << fixed;
    for (i = 1; i < top; i++)
        fout << a[st[i]].x << " " << a[st[i]].y << "\n";
    fout.close();
}

int main()
{
    Citire();
    Hill();
    return 0;
}