Advertisement
GeeckoDev

CodinGame

Oct 28th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. // CodinGame
  2. // Pseudo   geeckodev
  3. // Rang     37
  4. // Score    93%
  5. // Durée   02:17:41
  6.  
  7. // Exercice 1 (79min 12s)
  8. // Ne passe pas les tests 5 et 7 par manque de temps (scanf + espaces = enfer)
  9. // Lisibilité 95%: "Variable 'buf' is assigned a value that is never used (-5%)"
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13.  
  14. static char **chars = NULL;
  15. static int chars_w;
  16. static int chars_h;
  17. static const int chars_n = 26 + 1;
  18.  
  19. static char str[256] = {0};
  20.  
  21. void init()
  22. {
  23.     int i, j;
  24.     char buf[1024] = {0};
  25.    
  26.     scanf("%d\n", &chars_w);
  27.     scanf("%d\n", &chars_h);
  28.     scanf("%s", str);
  29.     getchar(); // Hack bizarre pour pouvoir lire le premier espace
  30.    
  31.     chars = malloc(chars_h * sizeof(char*));
  32.     for (i=0; i<chars_h; i++)
  33.     {
  34.         chars[i] = calloc(chars_n*(chars_w+1) + 1, sizeof(char));
  35.         scanf("%[^\n]\n", chars[i]);
  36.     }
  37.    
  38.     // On scinde les chaînes en insérant des \0
  39.     for (i=0; i<chars_h; i++)
  40.     {
  41.         for (j=1; j<chars_n+1; j++)
  42.         {
  43.             chars[i][j*chars_w-1] = '\0';
  44.         }
  45.     }
  46. }
  47.  
  48. char proper(char c)
  49. {
  50.     if (c >= 'a' && c <= 'z')
  51.         return c;
  52.    
  53.     if (c >= 'A' && c <= 'Z')
  54.         return c - ('A' - 'a');
  55.        
  56.     return 'z'+1;
  57. }
  58.  
  59. void printline(char c, int h)
  60. {
  61.     printf("%s ", chars[h]+(c-'a')*chars_w);
  62. }
  63.  
  64. void print()
  65. {
  66.     int i, j;
  67.     int len = strlen(str);
  68.    
  69.     for (i=0; i<chars_h; i++)
  70.     {
  71.         for (j=0; j<len; j++)
  72.         {
  73.             printline(proper(str[j]), i);
  74.         }
  75.         printf("\n");
  76.     }
  77. }
  78.  
  79. int main()
  80. {
  81.     init();
  82.     print();
  83.    
  84.     return 0;
  85. }
  86.  
  87. // Exercice 2 (32min 17s)
  88. // Algorithme terriblement compliqué par rapport à Sed
  89. // J'étais persuadé que c'était la manière la plus élégante de le faire
  90.  
  91. #include <stdlib.h>
  92. #include <stdio.h>
  93.  
  94. typedef struct
  95. {
  96.     int v;
  97.     int i;
  98. } Couple;
  99.  
  100. static int n;
  101. static int *v;
  102. static Couple *cp;
  103.  
  104. int compare(const void *a, const void *b)
  105. {
  106.     return (((Couple*)b)->v - ((Couple*)a)->v);
  107. }
  108.  
  109. void init()
  110. {
  111.     int i;
  112.    
  113.     scanf("%d\n", &n);
  114.    
  115.     v = malloc(n * sizeof(int));
  116.    
  117.     for (i=0; i<n; i++)
  118.         scanf("%d", v+i);
  119.        
  120.     cp = malloc(n * sizeof(Couple));
  121.    
  122.     for (i=0; i<n; i++)
  123.     {
  124.         cp[i].v = v[i];
  125.         cp[i].i = i;
  126.     }
  127.  
  128.     qsort(cp, n, sizeof(Couple), compare);
  129. }
  130.  
  131. int result()
  132. {
  133.     int i, j;
  134.     int loss = 0;
  135.    
  136.     for (i=0; i<n; i++)
  137.     {
  138.         for (j=n-1; cp[j].v<cp[i].v; j--)
  139.         {
  140.             if (cp[j].i > cp[i].i)
  141.             {
  142.                 if (cp[j].v-cp[i].v < loss)
  143.                     loss = cp[j].v-cp[i].v;
  144.                
  145.                 break;
  146.             }
  147.         }
  148.     }
  149.    
  150.     return loss;
  151. }
  152.  
  153. int main()
  154. {
  155.     init();
  156.    
  157.     printf("%d", result());
  158.  
  159.     return 0;
  160. }
  161.  
  162. // Exercice 3 (26min 13s)
  163. // Le plus réussi, même si le type 'long long' est superflu
  164.  
  165. #include <stdlib.h>
  166. #include <stdio.h>
  167.  
  168. typedef struct
  169. {
  170.     int x;
  171.     int y;
  172. } Point;
  173.  
  174. static int n;
  175. static Point *p;
  176.  
  177. int compare_x(const void *a, const void *b)
  178. {
  179.     return (((Point*)a)->x - ((Point*)b)->x);
  180. }
  181.  
  182. int compare_y(const void *a, const void *b)
  183. {
  184.     return (((Point*)a)->y - ((Point*)b)->y);
  185. }
  186.  
  187. void init()
  188. {
  189.     int i;
  190.    
  191.     scanf("%d\n", &n);
  192.    
  193.     p = malloc(n * sizeof(Point));
  194.    
  195.     for (i=0; i<n; i++)
  196.     {
  197.         scanf("%d %d\n", &p[i].x, &p[i].y);
  198.     }
  199. }
  200.  
  201. long long result()
  202. {
  203.     long long length;
  204.     int med;
  205.     int i;
  206.    
  207.     qsort(p, n, sizeof(Point), compare_x);
  208.    
  209.     length = p[n-1].x - p[0].x;
  210.    
  211.     qsort(p, n, sizeof(Point), compare_y);
  212.    
  213.     med = p[n/2].y;
  214.    
  215.     for (i=0; i<n; i++)
  216.     {
  217.         int l;
  218.        
  219.         l = p[i].y - med;
  220.        
  221.         length += (l > 0 ? l : -l);
  222.     }
  223.    
  224.     return length;
  225. }
  226.  
  227. int main()
  228. {
  229.     init();
  230.    
  231.     printf("%ld", result());
  232.  
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement