Cod sursa(job #171288)

Utilizator M@2Te4iMatei Misarca M@2Te4i Data 3 aprilie 2008 22:52:24
Problema Wanted Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <cstdio>
#define vv 205

using namespace std;

int n,w[vv][2];

void citire()
{
    freopen("wanted.in","r",stdin);
    scanf("%d", &n);
    for (int i=1; i<=n; i++)
    {
        scanf("%d%d", &w[i][0], &w[i][1]);
    }
}

void sortare(int l, int r)
{
int i,j,x,y;
i=l;
j=r;
x=w[(l+r)/2][0];
do
{
    while (w[i][0]<x)
        ++i;
    while (x<w[j][0])
        --j;
    if (i<=j)
    {
        y=w[i][0];
        w[i][0]=w[j][0];
        w[j][0]=y;
        y=w[i][1];
        w[i][1]=w[j][1];
        w[j][1]=y;
        ++i;
        --j;
    }
}
while (i<=j);
if (l<j)
    sortare(l,j);
if (i<r)
    sortare(i,r);
}

void rezolvare()
{
    int s,q,m=2100000000,e;
    for (int i=1; i<=n; i++)
    {
        if (w[i][0]<0)
            q=-w[i][0];
        else
            q=w[i][0];
        s=q+w[i][1];
        for (int j=i+1; j<=n; j++)
        {
            s+=w[j-1][1]+w[j][1];
            if (w[j-1][0]<0 && w[j][0]<0)
                s+=(w[j-1][0]*-1)-(w[j][0]*-1);
            else if (w[j-1][0]>0 && w[j][0]>0)
                s+=w[j][0]-w[j-1][0];
            else s+=(-1*w[j-1][0])+w[j][0];
        }
        e=s;
        if (w[i][0]<0)
            q=-w[i][0];
        else
            q=w[i][0];
        s=q+w[i][1];
        for (int j=i-1; j>=1; j--)
        {
            s+=w[j+1][1]+w[j][1];
            if (w[j+1][0]<0 && w[j][0]<0)
                s+=(w[j+1][0]*-1)-(w[j][0]*-1);
            else if (w[j][0]>0 && w[j+1][0]>0)
                s+=w[j+1][0]-w[j][0];
            else s+=w[j+1][0]+(w[j][0]*-1);
        }
        if (e>s)
        {
            if (m>e)
                m=e;
        }
        else
            if (m>s)
                m=s;

    }
    printf("%d\n", m);
}

int main()
{
    freopen("wanted.out","w",stdout);
    citire();
    sortare(1,n);
    rezolvare();
    return 0;
}