Cod sursa(job #982088)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 8 august 2013 14:31:53
Problema Tribute Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

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

const int N = 50010;
const long long oo = (1 << 30);

long long n, dx, dy, minx, miny,
frx[N], fry[N], st[N], dr[N], nrp[N];

int main()
{
    fin>>n>>dx>>dy;
    minx = miny = oo;
    for(int i=1; i <= n; i++)
    {
        int l, c; fin>>l>>c;
        frx[l]++, fry[c]++;
    }

    nrp[0] = frx[0];
    for(int i=1; i<N; i++)
    {
        st[i] = nrp[i-1] + st[i-1];
        nrp[i] = nrp[i-1] + frx[i];
    }

    for(int i=N; i>=0; i--)
    {
        dr[i] = nrp[i+1] + dr[i+1];
        nrp[i] = nrp[i+1] + frx[i];
    }

    for(int i=0; i+dx<N; i++)
        minx = min(minx, st[i] + dr[i+dx]);

    memset(nrp, 0, sizeof nrp);
    nrp[0] = fry[0];
    for(int i=1; i<N; i++)
    {
        st[i] = nrp[i-1] + st[i-1];
        nrp[i] = nrp[i-1] + fry[i];
    }

    for(int i=N; i>=0; i--)
    {
        dr[i] = nrp[i+1] + dr[i+1];
        nrp[i] = nrp[i+1] + fry[i];
    }

    for(int i=0; i+dy<N; i++)
        miny = min(miny, st[i] + dr[i+dy]);
    fout<<minx + miny;

    return 0;
}