gdandrea97

Reddit Daily Programmer #350 easy

Feb 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #define MAXSTR 1000
  5.  
  6. long longCompare(const void *a, const void *b) {
  7.     return -1 * (*(long *) a - *(long *) b);
  8. }
  9.  
  10. int read_input(char *filename, long **shelves, long **books, int *nShelves, int *nBooks, int *maxBooksWidth) {
  11.     FILE *in = fopen(filename, "r");
  12.     long tmp;
  13.     char *shelves_width_s = malloc(MAXSTR * sizeof(char));
  14.     int n = 0, N = 3;
  15.     *shelves = malloc(N * sizeof(long));
  16.  
  17.     int k = 0, K = 3;
  18.     *books = malloc(N * sizeof(long));
  19.  
  20.     // Read the shelves widths
  21.     fgets(shelves_width_s, MAXSTR, in);
  22.     char *tmpC = shelves_width_s;
  23.     while ((tmp = strtol(tmpC, &tmpC, 10)) != 0) {
  24.         if (n >= N) {
  25.             N *= 2;
  26.             *shelves = realloc(*shelves, N * sizeof(long));
  27.         }
  28.         *(*shelves + n++) = tmp;
  29.     }
  30.     *nShelves = n;
  31.  
  32.     //Read the books widths
  33.     while (fscanf(in, "%ld %*s", &tmp) == 1) {
  34.         if (k >= K) {
  35.             K *= 2;
  36.             *books = realloc(*books, K * sizeof(long));
  37.         }
  38.         *(*books + k++) = tmp;
  39.         (*maxBooksWidth) += tmp;
  40.     }
  41.     *nBooks = k;
  42.     free(shelves_width_s);
  43. }
  44.  
  45. int solve(long *shelves, const long *books, int nShelves, int nBooks, int maxBooksWidth) {
  46.     int currShelf = 0;
  47.     int *markBooks = malloc(nBooks * sizeof(int));
  48.     int *markShelves = malloc(nShelves * sizeof(int));
  49.     long *shelvesCopy = malloc(nShelves * sizeof(long));
  50.     memcpy(shelvesCopy, shelves, nShelves * sizeof(long));
  51.  
  52.     for (int k = 0; k < nBooks; k++)
  53.         markBooks[k] = 0;
  54.     for (int k = 0; k < nShelves; k++)
  55.         markShelves[k] = 0;
  56.  
  57.     for (int i = 0; i < nShelves && maxBooksWidth != 0; i++) {
  58.         for (int j = 0; j < nBooks && maxBooksWidth != 0; j++) {
  59.             if (!markBooks[j] && shelvesCopy[i] >= books[j]) {
  60.                 currShelf = i;
  61.                 markShelves[i] = 1;
  62.                 shelvesCopy[i] -= books[j];
  63.                 maxBooksWidth -= books[j];
  64.                 markBooks[j] = 1;
  65.             }
  66.         }
  67.     }
  68.     if (maxBooksWidth == 0) {
  69.         printf("Shelves to buy: %d\n", currShelf + 1);
  70.         for (int i = 0; i <= currShelf; i++)
  71.             printf("%ld ", shelves[i]);
  72.     } else
  73.         printf("Impossible\n");
  74. }
  75.  
  76. int main() {
  77.     long *shelves;    //Contains the shelves width
  78.     int nShelves;
  79.     long *books;    //Contains the books width
  80.     int nBooks;
  81.     int maxBooksWidth = 0;
  82.  
  83.     read_input("input.txt", &shelves, &books, &nShelves, &nBooks, &maxBooksWidth);
  84.     qsort(shelves, nShelves, sizeof(long), longCompare);
  85.     qsort(books, nBooks, sizeof(long), longCompare);
  86.     solve(shelves, books, nShelves, nBooks, maxBooksWidth);
  87.     return 0;
  88. }
Add Comment
Please, Sign In to add comment