Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct queue
  6. {
  7. int Pi;
  8. struct queue *next;
  9. struct queue *prev;
  10. int pos;
  11. };
  12.  
  13. /**
  14. * \breif Add an element to the queue
  15. * \param q The queue to add the element
  16. * \param Pi The data of the element
  17. * \param i The index of the element
  18. * \return The queue with the new element
  19. */
  20. struct queue *add_elt(struct queue *q, int Pi, int i)
  21. {
  22. struct queue *n = malloc(sizeof(*n));
  23. if (!n)
  24. return NULL;
  25. n->Pi = Pi;
  26. n->next = NULL;
  27. n->prev = q;
  28. n->pos = i;
  29. if (q)
  30. q->next = n;
  31. return n;
  32. }
  33.  
  34. /**
  35. * \breif Free the memory of the queue
  36. * \param q The queue to freed
  37. * \param N The size of the queue
  38. */
  39. void free_queue(struct queue *q, int N)
  40. {
  41. struct queue *tmp = q;
  42. while (tmp->pos != N - 1)
  43. tmp = tmp->next;
  44. tmp->next = NULL;
  45. while (q)
  46. {
  47. tmp = q;
  48. q = q->next;
  49. free(tmp);
  50.  
  51. }
  52. }
  53.  
  54. /**
  55. * \breif Check if a number is already in an array
  56. * \param t The array to check
  57. * \param pos the number to check
  58. * \param size The size of the array
  59. * \return Returns 0 if the number is not present and the position of the number if it is
  60. */
  61. int check_pos(int t[], int pos, int size)
  62. {
  63. for (int i = 0; i < size; i++)
  64. if (pos == t[i])
  65. return i;
  66. return 0;
  67. }
  68.  
  69. /**
  70. * \brief Function that fill the carousel
  71. * \param q struct queue that contains the number of people per group
  72. * \param L The max capacity of the carousel
  73. * \param N The max capacity of the queue
  74. * \param res A pointer on the total earnings
  75. * \return struct *queue Return the queue to keep the head at the good place
  76. */
  77. struct queue *fill(struct queue *q, int L, int N, long *res)
  78. {
  79. int max = L;
  80. for (;N > 0 && (L - q->Pi) >= 0; N--)
  81. {
  82. L -= q->Pi;
  83. q = q->next;
  84. }
  85. *res += max - L;
  86. return q;
  87. }
  88.  
  89. /**
  90. * \brief Function that computes the gains
  91. * \param q struct queue that contains the number of people per group
  92. * \param C The number of cycle possible
  93. * \param L The max capacity of the carousel
  94. * \param N The max capacity of the queue
  95. * \return Long, The sum of all the dirhams earned
  96. */
  97. long turn(struct queue *q, int C, int L, int N)
  98. {
  99. long res = 0;
  100. int m_C = C;
  101. int pos[N];
  102. long t_res[N];
  103. int cycle = 0;
  104. long dir = 0;
  105. for (; C > 0; C--)
  106. {
  107. int i = check_pos(pos, q->pos, m_C - C);
  108. if (!i)
  109. {
  110. pos[m_C - C] = q->pos;
  111. t_res[m_C - C] = res;
  112. }
  113. else
  114. {
  115. dir = res - t_res[i];
  116. cycle = m_C - (C + i);
  117. break;
  118. }
  119. q = fill(q, L, N, &res);
  120. }
  121. if (dir)
  122. {
  123. int mult = C / cycle;
  124. int mod_C = C % cycle;
  125. res += mult * dir;
  126. for(; mod_C > 0; mod_C--)
  127. q = fill(q, L, N, &res);
  128. }
  129. return res;
  130. }
  131.  
  132. int main()
  133. {
  134. int L;
  135. int C;
  136. int N;
  137. scanf("%d%d%d", &L, &C, &N);
  138. struct queue *q = NULL;
  139. for (int i = 0; i < N; i++) {
  140. int Pi;
  141. scanf("%d", &Pi);
  142. q = add_elt(q, Pi, i);
  143. }
  144. struct queue *tmp = q;
  145. while (tmp->prev)
  146. tmp = tmp->prev;
  147. q->next = tmp;
  148. q = tmp;
  149.  
  150. long res = turn(q, C, L, N);
  151. printf("%ld\n", res);
  152. free_queue(q, N);
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement