Advertisement
GeeckoDev

CodinGame n°3 (26min 13s)

Oct 28th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. // Read inputs from stdin. Write outputs to stdout.
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. typedef struct
  7. {
  8.     int x;
  9.     int y;
  10. } Point;
  11.  
  12. static int n;
  13. static Point *p;
  14.  
  15. int compare_x(const void *a, const void *b)
  16. {
  17.     return (((Point*)a)->x - ((Point*)b)->x);
  18. }
  19.  
  20. int compare_y(const void *a, const void *b)
  21. {
  22.     return (((Point*)a)->y - ((Point*)b)->y);
  23. }
  24.  
  25. void init()
  26. {
  27.     int i;
  28.    
  29.     scanf("%d\n", &n);
  30.    
  31.     p = malloc(n * sizeof(Point));
  32.    
  33.     for (i=0; i<n; i++)
  34.     {
  35.         scanf("%d %d\n", &p[i].x, &p[i].y);
  36.     }
  37. }
  38.  
  39. long long result()
  40. {
  41.     long long length;
  42.     int med;
  43.     int i;
  44.    
  45.     qsort(p, n, sizeof(Point), compare_x);
  46.    
  47.     length = p[n-1].x - p[0].x;
  48.    
  49.     qsort(p, n, sizeof(Point), compare_y);
  50.    
  51.     med = p[n/2].y;
  52.    
  53.     for (i=0; i<n; i++)
  54.     {
  55.         int l;
  56.        
  57.         l = p[i].y - med;
  58.        
  59.         length += (l > 0 ? l : -l);
  60.     }
  61.    
  62.     return length;
  63. }
  64.  
  65. int main()
  66. {
  67.     init();
  68.    
  69.     printf("%ld", result());
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement