Cod sursa(job #163587)

Utilizator za_wolfpalianos cristian za_wolf Data 22 martie 2008 14:48:16
Problema Wanted Scor 10
Compilator cpp Status done
Runda preONI 2008, Runda Finala, Clasa a 10-a Marime 2.47 kb
#include<stdio.h>
#include<math.h>
#include<algorithm>
#define NMAX 221
using namespace std;
long poz,in,sf,b,sol[NMAX],n,i,rez,pas,j,w,m,k,l,a,s,y[NMAX];
struct kkt
{
    long X,Y;
};
kkt x[NMAX];
struct kkkt
{
    long L,R;
};
kkkt z[NMAX];
int cmpf(const kkt a,const kkt b)
{
    return (a.X<b.X);
}
void rezolv(long l,long r,long pas)
{
    long min=20000,poz,q;
    a=0;

    for (int i=l;i<=r;i++)
        if (y[i]==0)
    {

        if (x[i].Y*2+2*labs(x[i].X-x[l].X)>x[i].Y*2+2*labs(x[r].X-x[i].X))
            q=x[i].Y*2+2*labs(x[i].X-x[l].X);
        else
            q=x[i].Y*2+2*labs(x[r].X-x[i].X);

        if (min>q)
        {
            min=q;
            poz=i;
        }
        a=1;
    }
    if (a)
    {
    w=poz;
    y[poz]=pas;
    }
}

void coi(long poz,long s,long pas)
{
    long p,a,ss;
    a=0;
    for (i=1;i<=poz;i++)
        if (y[i]==pas)
        {
            p=i;
            a=1;
        }
    if (a)
    {
        ss=s;
        ss+=labs(x[poz].X-x[p].X)+x[p].Y;
        if (ss<sol[p]||sol[p]==0)
            sol[p]=ss;
        ss+=x[p].Y;
        coi(p,ss,pas+1);
    }

    a=0;
    for (i=poz+1;i<=n;i++)
      if (y[i]==pas)
      {
            p=i;
            a=1;
      }
    if (a)
    {
        if (pas==1)
            pas=1;
        s+=labs(x[p].X-x[poz].X)+x[p].Y;
        if (s<sol[p]||sol[p]==0)
            sol[p]=s;
        s+=x[p].Y;
        coi(p,s,pas+1);
    }
    a=0;
}

int main()
{
    freopen("wanted.in","r",stdin);
    freopen("wanted.out","w",stdout);
    scanf("%ld",&n);
    for (i=1;i<=n;i++)
        scanf("%ld%ld",&x[i].X,&x[i].Y);

    sort(x+1,x+n+1,cmpf);
    i=1;
    pas=1;
    rezolv(1,n,pas);
    in=1;
    sf=2;
    z[1].L=1;
    z[1].R=w;
    z[2].L=w;
    z[2].R=n;
    b=sf;
    pas++;
    while (in<=sf)
    {
        rezolv(z[in].L,z[in].R,pas);
        
        if (a)
        {
            z[++sf].L=z[in].L;
            z[sf].R=w-1;
            z[++sf].L=w+1;
            z[sf].R=z[in].R;
        }
        if (in==b)
        {
            pas++;
            b=sf;
        }
        in++;
    }
    pas=1;
    for (i=1;i<=n;i++)
        if (y[i]==pas)
            poz=i;
    i=poz;
    rez=-1;
    sol[i]=labs(x[i].X)+labs(x[i].Y);
    coi(i,labs(x[i].X)+2*labs(x[i].Y),pas+1);
    rez=-1;
    for (i=1;i<=n;i++)
        if (rez<sol[i])
            rez=sol[i];
    printf("%ld\n",rez);
    return 0;
}