Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CodinGame
- // Pseudo geeckodev
- // Rang 37
- // Score 93%
- // Durée 02:17:41
- // Exercice 1 (79min 12s)
- // Ne passe pas les tests 5 et 7 par manque de temps (scanf + espaces = enfer)
- // Lisibilité 95%: "Variable 'buf' is assigned a value that is never used (-5%)"
- #include <stdlib.h>
- #include <stdio.h>
- static char **chars = NULL;
- static int chars_w;
- static int chars_h;
- static const int chars_n = 26 + 1;
- static char str[256] = {0};
- void init()
- {
- int i, j;
- char buf[1024] = {0};
- scanf("%d\n", &chars_w);
- scanf("%d\n", &chars_h);
- scanf("%s", str);
- getchar(); // Hack bizarre pour pouvoir lire le premier espace
- chars = malloc(chars_h * sizeof(char*));
- for (i=0; i<chars_h; i++)
- {
- chars[i] = calloc(chars_n*(chars_w+1) + 1, sizeof(char));
- scanf("%[^\n]\n", chars[i]);
- }
- // On scinde les chaînes en insérant des \0
- for (i=0; i<chars_h; i++)
- {
- for (j=1; j<chars_n+1; j++)
- {
- chars[i][j*chars_w-1] = '\0';
- }
- }
- }
- char proper(char c)
- {
- if (c >= 'a' && c <= 'z')
- return c;
- if (c >= 'A' && c <= 'Z')
- return c - ('A' - 'a');
- return 'z'+1;
- }
- void printline(char c, int h)
- {
- printf("%s ", chars[h]+(c-'a')*chars_w);
- }
- void print()
- {
- int i, j;
- int len = strlen(str);
- for (i=0; i<chars_h; i++)
- {
- for (j=0; j<len; j++)
- {
- printline(proper(str[j]), i);
- }
- printf("\n");
- }
- }
- int main()
- {
- init();
- print();
- return 0;
- }
- // Exercice 2 (32min 17s)
- // Algorithme terriblement compliqué par rapport à Sed
- // J'étais persuadé que c'était la manière la plus élégante de le faire
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct
- {
- int v;
- int i;
- } Couple;
- static int n;
- static int *v;
- static Couple *cp;
- int compare(const void *a, const void *b)
- {
- return (((Couple*)b)->v - ((Couple*)a)->v);
- }
- void init()
- {
- int i;
- scanf("%d\n", &n);
- v = malloc(n * sizeof(int));
- for (i=0; i<n; i++)
- scanf("%d", v+i);
- cp = malloc(n * sizeof(Couple));
- for (i=0; i<n; i++)
- {
- cp[i].v = v[i];
- cp[i].i = i;
- }
- qsort(cp, n, sizeof(Couple), compare);
- }
- int result()
- {
- int i, j;
- int loss = 0;
- for (i=0; i<n; i++)
- {
- for (j=n-1; cp[j].v<cp[i].v; j--)
- {
- if (cp[j].i > cp[i].i)
- {
- if (cp[j].v-cp[i].v < loss)
- loss = cp[j].v-cp[i].v;
- break;
- }
- }
- }
- return loss;
- }
- int main()
- {
- init();
- printf("%d", result());
- return 0;
- }
- // Exercice 3 (26min 13s)
- // Le plus réussi, même si le type 'long long' est superflu
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct
- {
- int x;
- int y;
- } Point;
- static int n;
- static Point *p;
- int compare_x(const void *a, const void *b)
- {
- return (((Point*)a)->x - ((Point*)b)->x);
- }
- int compare_y(const void *a, const void *b)
- {
- return (((Point*)a)->y - ((Point*)b)->y);
- }
- void init()
- {
- int i;
- scanf("%d\n", &n);
- p = malloc(n * sizeof(Point));
- for (i=0; i<n; i++)
- {
- scanf("%d %d\n", &p[i].x, &p[i].y);
- }
- }
- long long result()
- {
- long long length;
- int med;
- int i;
- qsort(p, n, sizeof(Point), compare_x);
- length = p[n-1].x - p[0].x;
- qsort(p, n, sizeof(Point), compare_y);
- med = p[n/2].y;
- for (i=0; i<n; i++)
- {
- int l;
- l = p[i].y - med;
- length += (l > 0 ? l : -l);
- }
- return length;
- }
- int main()
- {
- init();
- printf("%ld", result());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement