Cod sursa(job #1405339)

Utilizator Ionut228Ionut Calofir Ionut228 Data 29 martie 2015 01:55:24
Problema Rubarba Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.67 kb
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

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

const double eps = 1e-2;

int N, top;
double sol, l1, l2, arie;

struct puncte
{
    int x, y;
};
puncte V[100005], ST[100005];

int cross_product(puncte A, puncte B, puncte C)
{
    return (B.y - A.y) * (C.x - A.x) - (C.y - A.y) * (B.x - A.x);
}

bool cmp(puncte p1, puncte p2)
{
    return cross_product(V[1], p1, p2) < 0;
}

void sort_points()
{
    int pos = 1;
    for (int i = 1; i <= N; ++i)
        if (V[i].x < V[pos].x || (V[i].x == V[pos].x && V[i].y < V[pos].y))
            pos = i;
    swap(V[1], V[pos]);

    sort(V + 2, V + N + 1, cmp);
}

void convex_hull()
{
    sort_points();

    ST[1] = V[1];
    ST[2] = V[2];
    top = 2;

    for (int i = 3; i <= N; ++i)
    {
        while (top >= 2 && cross_product(ST[top - 1], ST[top], V[i]) > 0)
            --top;
        ST[++top] = V[i];
    }
}

int paralele (puncte A, puncte B, puncte C, puncte D)
{
    int a1 = B.y - A.y;
    int b1 = A.x - B.x;
    int a2 = D.y - C.y;
    int b2 = C.x - D.x;

    return (a1 * b2 == a2 * b1);
}

int perpendicularD1(puncte A, puncte B, puncte D)
{
    int a1 = B.y - A.y;
    int b1 = A.x - B.x;
    int a2 = A.y - D.y;
    int b2 = D.x - A.x;

    return (a1 * a2 == -(b1 * b2));
}

int perpendicularD2(puncte A, puncte B, puncte C)
{
    int a1 = B.y - A.y;
    int b1 = A.x - B.x;
    int a2 = C.y - B.y;
    int b2 = B.x - C.x;

    return (a1 * a2 == -(b1 * b2));
}

void solve()
{
    sol = -1.0;
    puncte A, B, C, D;
    ST[++top] = V[1];
    for (int i = 1; i <= top; ++i)
    {
        A = V[i];
        B = V[i + 1];
        for (int j = i + 2; j < top; ++j)
        {
            C = V[j];
            D = V[j + 1];

            if (paralele(A, B, C, D))
            {
                if (perpendicularD1(A, B, D) && perpendicularD2(A, B, C))
                {
                    l1 = sqrt(double(B.y - A.y) * double(B.y - A.y) + double(B.x - A.x) * double(B.x - A.x));
                    l2 = sqrt(double(C.y - B.y) * double(C.y - B.y) + double(C.x - B.x) * double(C.x - B.x));
                    arie = l1 * l2;

                    if (sol == -1.0)
                        sol = arie;
                    sol = min(sol, arie);
                }
            }
        }
    }
}

int main()
{
    fin >> N;
    for (int i = 1; i <= N; ++i)
        fin >> V[i].x >> V[i].y;

    convex_hull();
    solve();

    fout << fixed << setprecision(2) << sol << '\n';

    fin.close();
    fout.close();
    return 0;
}