Cod sursa(job #25622)

Utilizator alextheroTandrau Alexandru alexthero Data 4 martie 2007 13:10:58
Problema Magazin Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <cstdio>
#include <algorithm>
#include <cmath>

#define nmax 50005

using namespace std;

int x[nmax],y[nmax],n,m,d,p,a[nmax],suma[nmax];

int dist(int i,int j) {
    int frand = x[i];
    int fcol = y[i];
    int lrand = x[j];
    int lcol = y[j];
    return fabs(frand - lrand) * d + min(fcol + lcol,2 * (m + 1) - (fcol + lcol));
}

int main() {
    freopen("magazin.in","r",stdin);
    freopen("magazin.out","w",stdout);

    scanf("%d %d %d %d\n",&p,&n,&m,&d);


    for(int i = 1; i <= p; i++) scanf("%d %d\n",&x[i],&y[i]);

    x[0] = 1;
    y[0] = 0;
    x[p + 1] = n;
    y[p + 1] = 0;

    a[0] = suma[0] = 0;

    for(int i = 1; i <= p + 1; i++) {
        a[i] = i;
        suma[i] = suma[i - 1] + dist(a[i - 1],a[i]);
    }

    printf("%d\n",suma[p + 1]);
    
    return 0;   
}