Cod sursa(job #2096703)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 29 decembrie 2017 17:06:48
Problema Tribute Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <bits/stdc++.h>
#define MAXN 50001
#define MAXC 50001

#define BUF_SIZE 1 << 14
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
    if(pbuf==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fi);
        pbuf=0;
    }
    return buf[pbuf++];
}
inline int nextnum(){
    int a = 0;
    char c = nextch();
    while(!isdigit(c))
        c = nextch();
    while(isdigit(c)){
        a = a * 10 + c - '0';
        c = nextch();
    }
    return a;
}

int fx[1 + MAXC], fy[1 + MAXC];
int sx[1 + MAXC], sy[1 + MAXC];
long long tx[1 + MAXC], ty[1 + MAXC];
int main(){
    fi = fopen("tribute.in","r");
    fo = fopen("tribute.out","w");

    int n = nextnum(), dx = nextnum(), dy = nextnum();
    for(int i = 1; i <= n; i++){
        fx[nextnum() + 1] ++;
        fy[nextnum() + 1] ++;
    }
    for(int i = 1; i <= MAXC; i++){
        sx[i] = sx[i - 1] + fx[i];
        tx[i] = tx[i - 1] + fx[i] * i;
        sy[i] = sy[i - 1] + fy[i];
        ty[i] = ty[i - 1] + fy[i] * i;
    }

    long long minx, miny;
    minx = miny = 1000000000000000000;
    for(int i = 1; i <= 7; i++){
        long long x, y;
        x = sx[i - 1] * i - tx[i - 1] - (sx[MAXC] - sx[std::min(i + dx, MAXC)]) * (i + dx) + (tx[MAXC] - tx[std::min(i + dx, MAXC)]);
        y = sy[i - 1] * i - ty[i - 1] - (sy[MAXC] - sy[std::min(i + dy, MAXC)]) * (i + dy) + (ty[MAXC] - ty[std::min(i + dy, MAXC)]);
        if(x < minx) minx = x;
        if(y < miny) miny = y;
    }
    fprintf(fo,"%lld", minx + miny);

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