Cod sursa(job #2184921)

Utilizator calinfloreaCalin Florea calinflorea Data 24 martie 2018 11:56:10
Problema Gradina Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.99 kb
#include <bits/stdc++.h>
#define NMax 260
using namespace std;

ifstream fin ("gradina.in");
ofstream fout ("gradina.out");

struct DB
{
    int x, y;
};

DB a[NMax];
int n, ans, ultimuly;
char sol[NMax];
int st[NMax], st2[NMax];
bool v[NMax];
int top, top2, N, M;

inline void Citire()
{
    int i;

    fin >> n;

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

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

///impart in doua semiplane
inline void Imparte()
{
    int i, j;

    sort(a + 1, a + n + 1, Compar);

    for(j = 1; j <= n; j++)
    {
        ultimuly = a[j].y;
        for(i = 1; i <= n; i++)
        {
            if(ultimuly <= a[i].y)
                st[++N] = i;
            else
                st2[++M] = i;
        }

    }
}

///determinantul pentru determinarea semiplanului
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[j].x * a[i].y;
}

/// calculez aria celor doua poligoane
inline void Arie()
{
    int i, arie = 0, arie2 = 0;

    top++;
    st[top] = st[1];

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

    top2++;
    st2[top2] = st2[1];

    for(int i = 2; i <= top2; i++)
        arie2 += a[st2[i - 1]].x * a[st2[i]].y - a[st2[i - 1]].y * a[st[i]].x;

    if(abs(arie - arie2) < ans)
    {
        ans = abs(arie - arie2);
        for(i = 1; i <= N; i++)
            sol[st[i]] = 'I';

         for(i = 1; i <= M; i++)
            sol[st2[i]] = 'I';
    }
}
inline void Infasuratoare()
{
    int i;
    ///planul de jos
    st[1] = 1;
    st[2] = 2;
    top = 2;

    for(i = 3; i <= N; i++)
    {
        while(top > 1 && F(st[top - 1], st[top], i) < 0)
        {
            v[st[top]] = 0;
            top--;
        }
        st[++top] = i;
        v[i] = 1;
    }

    for(i = N - 1; i >= 1; i--)
        if(v[i] == 0)
        {
            while(F(st[top - 1], st[top], i) < 0)
            {
                v[st[top]] = 0;
                top--;
            }
            st[++top] = i;
            v[i] = 1;
        }

    ///planul de sus
    st2[1] = 1;
    st2[2] = 2;
    top2 = 2;

    for(i = 3; i <= M; i++)
    {
        while(top > 1 && F(st2[top - 1], st2[top], i) < 0)
        {
            v[st2[top2]] = 0;
            top2--;
        }
        st2[++top2] = i;
        v[i] = 1;
    }

    for(i = M - 1; i >= 1; i--)
        if(v[i] == 0)
        {
            while(F(st2[top2 - 1], st2[top2], i) < 0)
            {
                v[st2[top2]] = 0;
                top2--;
            }
            st2[++top2] = i;
            v[i] = 1;
        }

    for(i = 1; i <= M; i++)
        if(v[i] == 0)
            return;

    Arie();
}

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