Cod sursa(job #1703783)

Utilizator a.pop256Andrei Popescu a.pop256 Data 17 mai 2016 17:25:41
Problema Tribute Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#define  MLC
#include <bits/stdc++.h>
using namespace std;

const int L   = 50005;
const int INF = 1e9;

int   fx[L],   fy[L],
    pstx[L], pdrx[L],
    psty[L], pdry[L],
    dstx[L], ddrx[L],
    dsty[L], ddry[L];

int main(void) {
    FILE *fi = fopen("tribute.in", "r");
    FILE *fo = fopen("tribute.out", "w");
    int n, x, y, dx, dy, sx=INF, sy=INF;

    fscanf(fi,"%d%d%d",&n,&dx,&dy);
    for(int i=1; i<=n; ++i) {
        fscanf(fi,"%d%d",&x,&y);
        ++fx[x];
        ++fy[y];
    }

    for(int i=1; i<L; ++i) {
        pstx[i] = pstx[i-1] + fx[i-1];
        psty[i] = psty[i-1] + fy[i-1];
    }
    for(int i=1; i<L; ++i) {
        dstx[i] = dstx[i-1] + pstx[i];
        dsty[i] = dsty[i-1] + psty[i];
    }

    for(int i=L-3; i>=0; --i) {
        pdrx[i] = pdrx[i+1] + fx[i+1];
        pdry[i] = pdry[i+1] + fy[i+1];
    }
    for(int i=L-3; i>=0; --i) {
        ddrx[i] = ddrx[i+1] + pdrx[i];
        ddry[i] = ddry[i+1] + pdry[i];
    }

    for(int i=0; i<L-dx-2; ++i)
        sx = min(sx, dstx[i]+ddrx[i+dx]);
    for(int i=0; i<L-dy-2; ++i)
        sy = min(sy, dsty[i]+ddry[i+dy]);


    fprintf(fo,"%d\n",sx+sy);

    fclose(fi);
    fclose(fo);
    return 0;
}