Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- struct queue
- {
- int Pi;
- struct queue *next;
- struct queue *prev;
- int pos;
- };
- /**
- * \breif Add an element to the queue
- * \param q The queue to add the element
- * \param Pi The data of the element
- * \param i The index of the element
- * \return The queue with the new element
- */
- struct queue *add_elt(struct queue *q, int Pi, int i)
- {
- struct queue *n = malloc(sizeof(*n));
- if (!n)
- return NULL;
- n->Pi = Pi;
- n->next = NULL;
- n->prev = q;
- n->pos = i;
- if (q)
- q->next = n;
- return n;
- }
- /**
- * \breif Free the memory of the queue
- * \param q The queue to freed
- * \param N The size of the queue
- */
- void free_queue(struct queue *q, int N)
- {
- struct queue *tmp = q;
- while (tmp->pos != N - 1)
- tmp = tmp->next;
- tmp->next = NULL;
- while (q)
- {
- tmp = q;
- q = q->next;
- free(tmp);
- }
- }
- /**
- * \breif Check if a number is already in an array
- * \param t The array to check
- * \param pos the number to check
- * \param size The size of the array
- * \return Returns 0 if the number is not present and the position of the number if it is
- */
- int check_pos(int t[], int pos, int size)
- {
- for (int i = 0; i < size; i++)
- if (pos == t[i])
- return i;
- return 0;
- }
- /**
- * \brief Function that fill the carousel
- * \param q struct queue that contains the number of people per group
- * \param L The max capacity of the carousel
- * \param N The max capacity of the queue
- * \param res A pointer on the total earnings
- * \return struct *queue Return the queue to keep the head at the good place
- */
- struct queue *fill(struct queue *q, int L, int N, long *res)
- {
- int max = L;
- for (;N > 0 && (L - q->Pi) >= 0; N--)
- {
- L -= q->Pi;
- q = q->next;
- }
- *res += max - L;
- return q;
- }
- /**
- * \brief Function that computes the gains
- * \param q struct queue that contains the number of people per group
- * \param C The number of cycle possible
- * \param L The max capacity of the carousel
- * \param N The max capacity of the queue
- * \return Long, The sum of all the dirhams earned
- */
- long turn(struct queue *q, int C, int L, int N)
- {
- long res = 0;
- int m_C = C;
- int pos[N];
- long t_res[N];
- int cycle = 0;
- long dir = 0;
- for (; C > 0; C--)
- {
- int i = check_pos(pos, q->pos, m_C - C);
- if (!i)
- {
- pos[m_C - C] = q->pos;
- t_res[m_C - C] = res;
- }
- else
- {
- dir = res - t_res[i];
- cycle = m_C - (C + i);
- break;
- }
- q = fill(q, L, N, &res);
- }
- if (dir)
- {
- int mult = C / cycle;
- int mod_C = C % cycle;
- res += mult * dir;
- for(; mod_C > 0; mod_C--)
- q = fill(q, L, N, &res);
- }
- return res;
- }
- int main()
- {
- int L;
- int C;
- int N;
- scanf("%d%d%d", &L, &C, &N);
- struct queue *q = NULL;
- for (int i = 0; i < N; i++) {
- int Pi;
- scanf("%d", &Pi);
- q = add_elt(q, Pi, i);
- }
- struct queue *tmp = q;
- while (tmp->prev)
- tmp = tmp->prev;
- q->next = tmp;
- q = tmp;
- long res = turn(q, C, L, N);
- printf("%ld\n", res);
- free_queue(q, N);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement