Cod sursa(job #167048)

Utilizator raula_sanChis Raoul raula_san Data 28 martie 2008 21:40:44
Problema Popandai Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.23 kb
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>

#define maxN 301

#define pb push_back
#define mp make_pair

#define x first
#define y second

using namespace std;

int N, K;
int Nr[maxN][maxN];

vector < pair <int, int> > P;

long long R;
long long U[maxN], D[maxN];

long long Mod(long long a)
{
    return
        a < 0 ? -a : a;
}

long long Area(int x0, int y0, int x1, int y1, int x2, int y2)
{
    return
        Mod ((x0 * y1) + (x1 * y2) + (x2 * y0) - (y0 * x1) - (y1 * x2) - (y2 * x0));
}

int Prod(int x0, int y0, int x1, int y1, int x2, int y2)
{
    return
        (x2-x0) * (y1-y0) - (x1-x0) * (y2-y0);
}

void Swap(int &i, int &j)
{
    int t = i;
    i = j;
    j = t;
}

void ReadData()
{
    freopen("popandai.in", "rt", stdin);

    int i, x, y;
    for(scanf("%d %d", &N, &K), i=1; i<=N; ++i)
    {
        scanf("%d %d", &x, &y);
        P.pb(mp(x, y));
    }

    sort(P.begin(), P.end());

    fclose(stdin);
}

void PreCompute()
{
    int i, j, k;
    for(i=0; i<N-2; ++i)
        for(j=i+2; j<N; ++j)
        {
            for(k=i+1; k<j; ++k)
                if(Prod(P[i].x ,P[i].y, P[j].x, P[j].y, P[k].x, P[k].y) > 0)
                {
                    ++ Nr[i][j];
                    ++ Nr[j][i];
                }
        }
}

void Dynamics()
{
    int i, j, k;

    R = 0x3f3f3f3f;

    for(i=0; i<=N; ++i)
        D[i] = U[i] = 0x3f3f3f3f;

    for(i=0; i<N-1; ++i)
        for(j=i+1; j<N; ++j)
        {
            for(k=0; k<=N; ++k)
                D[k] = U[k] = 0x3f3f3f3f;

            for(k=0; k<N; ++k) if(k != i && k != j)
            {
                int x0 = P[i].x, y0 = P[i].y, x1 = P[j].x, y1 = P[j].y, x2 = P[k].x, y2 = P[k].y;

                int a = i, b = j, c = k;

                if(x0 > x1)
                {
                    Swap(x0, x1);
                    Swap(y0, y1);
                    Swap(a, b);
                }
                if(x0 > x2)
                {
                    Swap(x0, x2);
                    Swap(y0, y2);
                    Swap(a, c);
                }
                if(x1 > x2)
                {
                    Swap(x1, x2);
                    Swap(y1, y2);
                    Swap(b, c);
                }

                int s = Prod(x0, y0, x2, y2, x1, y1);

                if(s > 0)
                {
                    int x = Nr[a][c] - Nr[a][b] - Nr[b][c] - 1;

                    long long a = Area(x0, y0, x1, y1, x2, y2);

                    if(D[x] > a) D[x] = a;
                }
                else
                {
                    int x = Nr[a][b] + Nr[b][c] - Nr[a][c];

                    long long a = Area(x0, y0, x1, y1, x2, y2);

                    if(U[x] > a) U[x] = a;
                }
            }

            for(k=N-1; k>=0; --k)
            {
                if(D[k+1] < D[k]) D[k] = D[k+1];
                if(U[k+1] < U[k]) U[k] = U[k+1];
            }

            for(k=0; k<=K; ++k)
                if(D[k] + U[K-k] < R) R = D[k] + U[K-k];
        }
}

void Write()
{
    freopen("popandai.out", "wt", stdout);

    if(R & 1) printf("%lld.5", R>>1);
    else
        printf("%lld.0", R>>1);

    fclose(stdout);
}

int main()
{
    ReadData();
    PreCompute();
    Dynamics();
    Write();

    return 0;
}