Cod sursa(job #2184922)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 24 martie 2018 11:56:13
Problema Gradina Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.8 kb
#include <bits/stdc++.h>
#define inf 100000000
using namespace std;
ifstream fin("gradina.in");
ofstream fout("gradina.out");

struct db {
    int x, y;
};

db a[260];

int n, st[260];
bool viz[260];

inline bool Cmp(const db A, const db B)
{
    if(A.y == B.y)
        return A.x < B.x;
    return A.y < B.y;
}

void Citire()
{
    int x, y;

    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> x >> y;
        a[i] = {x, y};
    }

    sort(a + 1, a + n + 1, Cmp);
}

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

double RezolvareSus(int p)
{
    int top;

    st[p] = 1;
    viz[p] = 1;
    st[p + 1] = 2;
    top = 2;

    for(int i = p + 2; i <= n; i++)
    {
        while(top > 1 && F(st[top - 1], st[top], i) < 0)
        {
            viz[st[top]] = 0;
            top--;
        }
        viz[i] = 1;
        st[++top] = i;
    }

    for(int i = n - 1; i >= p; i--)
        if(!viz[i])
        {
            while(top > 1 && F(st[top - 1], st[top], i) < 0)
            {
                viz[st[top]] = 0;
                top--;
            }
            viz[i] = 1;
            st[++top] = i;
        }

    /// arie poligon convex obtinut in zona superioara
    long double arie = 0.0;

    for(int i = 2; i < top; i++)
        arie += a[i - 1].x * a[i].y - a[i - 1].y * a[i].x;

    return arie / 2.0;
}

double RezolvareJos(int p)
{
    int top;

    st[1] = 1;
    viz[1] = 1;
    st[2] = 2;
    top = 2;

    for(int i = 1; i < p; i++)
    {
        while(top > 1 && F(st[top - 1], st[top], i) < 0)
        {
            viz[st[top]] = 0;
            top--;
        }
        viz[i] = 1;
        st[++top] = i;
    }

    for(int i = p - 2; i >= 1; i--)
        if(!viz[i])
        {
            while(top > 1 && F(st[top - 1], st[top], i) < 0)
            {
                viz[st[top]] = 0;
                top--;
            }
            viz[i] = 1;
            st[++top] = i;
        }

    /// arie poligon convex obtinut in zona inferioara
    long double arie = 0.0;

    for(int i = 2; i < top; i++)
        arie += a[i - 1].x * a[i].y - a[i - 1].y * a[i].x;

    return arie / 2.0;
}

void Problema()
{
    long double minim = inf;
    int poz;

    for(int i = 1; i <= n; i++)
    {
        long double difArie = abs(RezolvareSus(a[i].y) - RezolvareJos(a[i].y));
        cout << RezolvareSus(a[i].y) << " " << RezolvareJos(a[i].y) << "\n";
        if(difArie < minim)
        {
            minim = difArie;
            poz = i;
        }
    }

    fout << minim << " " << poz << "\n";
}

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