Cod sursa(job #1591865)

Utilizator DiClauDan Claudiu DiClau Data 6 februarie 2016 19:45:13
Problema Tribute Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include<stdio.h>
using namespace std;
const int N = 50005;
int pctx[N], pcty[N], vx[N], vy[N];
int abs (int x)
{
    if (x < 0)
        return -x;
    return x;
}
int minim (int a, int b)
{
    if (a < b)
        return a;
    return b;
}
int main ()
{
    FILE *in, *out;
    in = fopen ("tribute.in", "r");
    out = fopen ("tribute.out", "w");
    int n, dX, dY;
    fscanf (in, "%d%d%d", &n, &dX, &dY);
    int x, y, i;
    for (i = 1; i <= n; i++)
    {
        fscanf (in, "%d%d", &x, &y);
        pctx[x]++;
        pcty[y]++;
        vx[i] = x;
        vy[i] = y;
    }
    int st = 0, dr = n;
    for (i = 0; i <= dX; i++)
        dr -= pctx[i];
    int pozx = 0;
    while (st < dr)
    {
        pozx++;
        dr -= pctx[pozx + dX];
        st += pctx[pozx - 1];
    }
    if (pctx[pozx - 1] > pctx[pozx + dX - 1])
        pozx--;
    int sus = n, jos = 0;
    for (i = 0; i <= dY; i++)
        sus -= pcty[i];
    int pozy = 0;
    while (jos < sus)
    {
        pozy++;
        sus -= pcty[pozy + dY];
        jos += pcty[pozy - 1];
    }
    if (pcty[pozy - 1] > pcty[pozy + dY - 1])
        pozy--;
    int s = 0;
    for (i = 1; i <= n; i++)
    {
        if (vx[i] < pozx)
            s += pozx - vx[i];
        if (vx[i] > pozx + dX)
            s += -pozx - dX + vx[i];
        if (vy[i] < pozy)
            s += pozy - vy[i];
        if (vy[i] > pozy + dY)
            s += -pozy - dY + vy[i];
    }
    fprintf (out, "%d", s);
    return 0;
}