Cod sursa(job #1709415)

Utilizator alexandra_paticaAndreea Alexandra Patica alexandra_patica Data 28 mai 2016 12:09:21
Problema Metrou4 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.64 kb
#include <cstdio>
#include <algorithm>
using namespace std;
//ifstream f ("metrou4.in");
//ofstream g ("metrou4.out");
struct loc
{
    int x, y;
}a[150001];
int n, t, i, j, l, k;
bool ap[150001];
int cmp (loc u, loc v)
{
    return (u.x<v.x || (u.x==v.x && u.y<v.y));
}
int main ()
{
    freopen("metrou4.in", "r", stdin);
    freopen ("metrou4.out", "w", stdout);
    scanf("%d", &t);
//    f >> t;
    for (j=1; j<=t; j++){
            scanf("%d", &n);
//        f >> n;
        for (i=1; i<=n; i++)
            scanf("%d%d", &a[i].x, &a[i].y);
//            f >> a[i].x >> a[i].y;
        sort(a+1,a+n+1,cmp);
//        for (i=1; i<=n; i++)
//            g << a[i].x << " " << a[i].y << '\n';
//        g << '\n';
        k=0;
        ap[1]=0;
        for (i=1; i<=n; i++){
//            if (ap[i]==0){
                for (l=i+1; l<=n; l++){
//                    if (i!=l){
                    if (ap[l]==0){
                        if (a[i].x==a[l].x) {
                            k=k+a[l].y-a[i].y;
                            ap[l]=1;
                        }
                        else if (a[i].y==a[l].y) {
                            k=k+a[l].x-a[i].x;
                            ap[l]=1;
                        }
                    }
//                    }
                }
//            }
        }
        for (i=2; i<=n; i++){
            if (ap[i]==0){
                k=k+abs(a[i-1].x-a[i].x)+abs(a[i-1].y-a[i].y);
//                ap[i]=1;
            }
        }
        printf("%d\n", k);
//        g << k << '\n';
        for (i=1; i<=n; i++)
            ap[i]=0;
    }
    return 0;
}