Cod sursa(job #943592)

Utilizator mitrutstrutMitrea Andrei Ionut mitrutstrut Data 25 aprilie 2013 22:00:06
Problema Tribute Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <string.h>
using namespace std;
#define MAX_N 50001
 
int main()
{
    ifstream f("tribute.in");
    ofstream g("tribute.out");
     
    int n, i, j, l, L, x[MAX_N], y[MAX_N], d1=1, d2=0, sum_x=0, sum_y=0, st, dr, sus, jos;
     
    //Read
    f>>n>>l>>L;
    for(i=1; i<=n; i++)
        f>>x[i]>>y[i];
         
    //Compute
    sort(x+1,x+n+1);
    sort(y+1,y+n+1);
    //lungime
    st = x[1]; dr = x[n];
    while(dr-st > l)
    {
        for(i=d1; x[i]<st+1; i++);
        for(j=n-d2; x[j]>dr-1; j--);
        d1 = i-1; d2 = n-j;
        if(d1 < d2)
        {
            st++;
            sum_x += d1;
        }
        else
        {
            dr--;
            sum_x += d2;
        }
    }
    //latime
    jos = y[1]; sus = y[n];
    d1 = 1; d2 = 0;
    while(sus - jos > L)
    {
        for(i=d1; y[i]<jos+1; i++);
        for(j=n-d2; y[j]>sus-1; j--);
        d1 = i-1; d2 = n-j;
        if(d1 < d2)
        {
            jos++;
            sum_y += d1;
        }
        else
        {
            sus--;
            sum_y += d2;
        }
    }
     
    //Print
    g<<sum_x + sum_y;
}