Advertisement
GeeckoDev

CodinGame n°2 (32min 17s)

Oct 28th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 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 v;
  9.     int i;
  10. } Couple;
  11.  
  12. static int n;
  13. static int *v;
  14. static Couple *cp;
  15.  
  16. int compare(const void *a, const void *b)
  17. {
  18.     return (((Couple*)b)->v - ((Couple*)a)->v);
  19. }
  20.  
  21. void init()
  22. {
  23.     int i;
  24.    
  25.     scanf("%d\n", &n);
  26.    
  27.     v = malloc(n * sizeof(int));
  28.    
  29.     for (i=0; i<n; i++)
  30.         scanf("%d", v+i);
  31.        
  32.     cp = malloc(n * sizeof(Couple));
  33.    
  34.     for (i=0; i<n; i++)
  35.     {
  36.         cp[i].v = v[i];
  37.         cp[i].i = i;
  38.     }
  39.  
  40.     qsort(cp, n, sizeof(Couple), compare);
  41. }
  42.  
  43. int result()
  44. {
  45.     int i, j;
  46.     int loss = 0;
  47.    
  48.     for (i=0; i<n; i++)
  49.     {
  50.         for (j=n-1; cp[j].v<cp[i].v; j--)
  51.         {
  52.             if (cp[j].i > cp[i].i)
  53.             {
  54.                 if (cp[j].v-cp[i].v < loss)
  55.                     loss = cp[j].v-cp[i].v;
  56.                
  57.                 break;
  58.             }
  59.         }
  60.     }
  61.    
  62.     return loss;
  63. }
  64.  
  65. int main()
  66. {
  67.     init();
  68.    
  69.     printf("%d", result());
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement