#include #include #include #include int get_line(char *s, int max) { int i; fgets(s,max,stdin); i = strlen(s) - 1; while (i >= 0 && s[i] == '\n') i--; i++; s[i] = 0; return i; } int sgetint(char **s, int *x) { char *t = *s; while (isspace(*t)) t++; if (sscanf(t,"%d",x) != 1) return 0; while (*t && !isspace(*t)) t++; *s = t; return 1; } struct point { int coord; struct point * next; }; struct point pts[200010]; int nextpt; struct point * xx[10002]; struct point * yy[10002]; int pole[10002]; int vysyp(struct point * list, int * pole) { int * q = pole; while (list) { *q++ = list->coord; list = list->next; } return q - pole; } int komparator(int *a, int *b) { return (*a<*b) ? -1 : (*a > *b); } int projdi(int * a, int i) /* a[i] je citelne */ { int total =0; int run = *a++; i >>= 1; while (i--) { total += *a++ - run; run = *a++; } return total; } /* int lowcoord(struct point * list, int hranice) { register int last = -1; register int c; while (list) { c = list->coord; if ((c < hranice) && (c > last)) { last = c; } list = list->next; } return last; } int highcoord(struct point * list, int hranice) { register int last = 20000; register int c; register struct point * pp = NULL; while (list) { c = list->coord; if ((c > hranice) && (c < last)) { last = c; pp = list; } list = list->next; } if (pp) { if (pp->next) { pp->coord = pp->next->coord; pp->next = pp->next->next; } else { pp->coord = -10; } } return last; } */ int main(void) { int P,i,x,y,total; struct point * q; for (;;) { scanf("%d\n", &P); if (!P) break; memset(xx,0,sizeof(xx)); memset(yy,0,sizeof(yy)); nextpt = 0; for (i=0 ; i < P ; i++) { scanf("%d %d\n",&x,&y); q = xx[x]; pts[nextpt].next = q; pts[nextpt].coord = y; xx[x] = pts + nextpt++; q = yy[y]; pts[nextpt].next = q; pts[nextpt].coord = x; yy[y] = pts + nextpt++; } total = 0; for (x = 0 ; x < 10002 ; x++) { if (xx[x]) { i = vysyp(xx[x],pole); qsort(pole, i, sizeof(pole[0]), komparator); total += projdi(pole, i); } } for (x = 0 ; x < 10002 ; x++) { if (yy[x]) { i = vysyp(yy[x],pole); qsort(pole, i, sizeof(pole[0]), komparator); total += projdi(pole, i); } } printf("The length of the fence will be %d units.\n", total); } return 0; }