Cod sursa(job #1694774)

Utilizator akaprosAna Kapros akapros Data 25 aprilie 2016 22:31:13
Problema Rubarba Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.93 kb
#include <bits/stdc++.h>
#define maxN 100002
#define ll long long
#define e 1e-12
using namespace std;
int n, head, st[maxN], Next[maxN];
bool vis[maxN];
double ans;
struct Point
{
    int x, y;
} v[maxN];
int cmp(const Point a, const Point b)
{
    if (a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}
ll cross_product(Point O, Point A, Point B)
{
    return 1LL * (A.x - O.x) * (B.y - O.y) - 1LL * (B.x - O.x) * (A.y - O.y);
}
Point newc(int x, int y)
{
    Point ans;
    ans.x = v[y].x - v[x].x;
    ans.y = v[y].y - v[x].y;
    return ans;
}
ll DProd(Point A, Point B)
{
    return 1LL * A.x * B.x + 1LL * A.y * B.y;
}
ll CProd(Point A, Point B)
{
    return 1LL * A.x * B.y - 1LL * A.y * B.x;
}
double Dist(int x, int y, double m)
{
    return fabs((1.0 * v[x].y - v[x].x * m) - (1.0 * v[y].y - v[y].x * m)) / sqrt(m * m + 1);
}
void convex_hull()
{
    int i, p;
    sort(v + 1, v + n + 1, cmp);

    st[1] = 1;
    st[2] = 2;
    head = 2;
    vis[2] = 1;

    for (i = 1, p = 1; i > 0; i += (p = (i == n ? -p : p)))
    {
        if (vis[i])
            continue;
        while (head >= 2 && cross_product(v[st[head - 1]], v[st[head]], v[i]) < 1LL * e)
            vis[st[head --]] = 0;
        st[++ head] = i;
        vis[i] = 1;
    }
    -- head;
}
void read()
{
    int i;
    freopen("rubarba.in", "r", stdin);
    scanf("%d", &n);
    for (i = 1; i <= n; ++ i)
        scanf("%d %d", &v[i].x, &v[i].y);
}
void solve()
{
    int i, p1, p2, p3;
    double Dx, Dy;
    convex_hull();
    for (i = 1; i < head; ++ i)
        Next[i] = i + 1;
    Next[head] = 1;
    p1 = 1;
    for (i = 1; i <= head; ++ i)
    {
        while (DProd(newc(st[i], st[Next[i]]), newc(st[p1], st[Next[p1]])) > 0LL)
              p1 = Next[p1];
        if (i == 1)
            p2 = p1;
        while (CProd(newc(st[i], st[Next[i]]), newc(st[p2], st[Next[p2]])) > 0LL)
              p2 = Next[p2];
        if (i == 1)
            p3 = p2;
        while (DProd(newc(st[i], st[Next[i]]), newc(st[p3], st[Next[p3]])) < 0LL)
              p3 = Next[p3];
        if (v[st[i]].x == v[st[Next[i]]].x)
        {
            Dx = fabs(v[st[i]].x - v[st[p2]].x);
            Dy = fabs(v[st[p1]].y - v[st[p3]].y);
        }else
        if (v[st[i]].y == v[st[Next[i]]].y)
        {
            Dx = fabs(v[st[p1]].x - v[st[p3]].x);
            Dy = fabs(v[st[i]].y - v[st[p2]].y);
        }else
        {
            double m = 1.0 * (v[st[Next[i]]].y - v[st[i]].y) / (v[st[Next[i]]].x - v[st[i]].x);
            Dx = Dist(st[i], st[p2], m);
            Dy = Dist(st[p1], st[p3], (-1.0 / m));
        }
        if (i == 1)
            ans = Dx * Dy;
        else
            if (Dx * Dy < ans)
               ans = Dx * Dy;
    }
}
void write()
{
    freopen("rubarba.out", "w", stdout);
    printf("%.2lf", ans);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}