Skip to content

Instantly share code, notes, and snippets.

@Amorim33
Created January 3, 2023 01:58
Show Gist options
  • Save Amorim33/54522f080ec46157e183b6c1d59eddc7 to your computer and use it in GitHub Desktop.
Save Amorim33/54522f080ec46157e183b6c1d59eddc7 to your computer and use it in GitHub Desktop.
Algorithm to solve elderly queue problem
// A commercial establishment implemented a service system with a single queue,
// in which people always enter at the end of the queue, except if they are elderly (those over
// 59 years old), who always enter before the person younger than him who is closest to the
// start of the queue.
// Implement a program to control this queue, in order to meet the requirements
// above.
// The program must read a sequence of people, each one with their age and the time they entered.
// Considering that each person is attended in 3 minutes, the program must print the queue at each time a person enters it.
#include<stdio.h>
typedef struct Person{
int age;
int time;
struct Person* next;
} Person;
typedef struct Queue{
Person* begin;
Person* end;
} Queue;
void quicksort(Person incomePeople[1000],int first,int last){
int i, j, pivot;
Person temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(incomePeople[i].time <= incomePeople[pivot].time && i < last)
i++;
while(incomePeople[j].time > incomePeople[pivot].time)
j--;
if(i<j){
temp = incomePeople[i];
incomePeople[i] = incomePeople[j];
incomePeople[j] = temp;
}
}
temp = incomePeople[pivot];
incomePeople[pivot] = incomePeople[j];
incomePeople[j] = temp;
quicksort(incomePeople, first, j-1);
quicksort(incomePeople, j+1, last);
}
}
int main(){
int n; // number of people
scanf("%d", &n);
Person people[n]; // array of people
Queue queue; // queue of people
for(int i = 0; i < n; i++){
scanf("%d %d", &people[i].age, &people[i].time);
}
quicksort(people, 0, n-1); // sort people by income time
queue.begin = &people[0];
queue.end = &people[0];
for (int i = 0; i < n; i++){
// remove already attended people from beggining of the queue
int attendanceTime = queue.begin->time;
while(people[i].time - attendanceTime > 3){
queue.begin = queue.begin->next;
attendanceTime += 3;
}
// insert new person in the queue
if (people[i].age <= 59){
queue.end->next = &people[i];
queue.end = &people[i];
}
// if the person is older than 59, insert it in the right place
else{
Person* aux = queue.begin;
int insertedFlag = 0;
while (aux != queue.end){
if (aux->next->age < people[i].age){
people[i].next = aux->next;
aux->next = &people[i];
insertedFlag = 1;
break;
}
aux = aux->next;
}
if (!insertedFlag){
queue.end->next = &people[i];
queue.end = &people[i];
}
}
// print queue if the income time of the next person is different or if it is the last person
if (people[i].time != people[i + 1].time || i == n - 1){
Person* aux = queue.begin;
while(1){
printf("%d ", aux->age);
if (aux == queue.end){
printf("\n");
break;
}
aux = aux->next;
}
}
}
return 0;
}
@AlexandreHiroyuki
Copy link

Thanks for helping me with this problem!

@AlexandreHiroyuki
Copy link

@AlexandreHiroyuki
Copy link

AlexandreHiroyuki commented Jan 3, 2023

Elderly Queue

A commercial establishment implemented a service system with a single queue,
in which people always enter at the end of the queue, except if they are elderly (those over
59 years old), who always enter before the person younger than him who is closest to the
start of the queue.
Implement a program to control this queue, in order to meet the requirements
above.
The program must read a sequence of people, each one with their age and the time they entered.
Considering that each person is attended in 3 minutes, the program must print the queue at each time a person enters it.

Input

The input is composed of an integer N (1 ≤ N ≤ 10^3), corresponding to the number of people who will enter the queue. The next N lines contain two values Ij, Tj, (10 ≤ Ij ≤ 90 and 0 ≤ Tj ≤ 300) corresponding to the age of each person and the time when that person arrives
in queue.

Output

Considering that each person is served in 3 minutes, the output of your program must have a line with the age of the person at the checkout, followed by the ages of the people in line, each
time someone enters the queue.

Restrictions

• If Ti = Tj, consider only one entry, already sorted by age priority.
• If a Ti value coincides with the end of service at the cashier, first change the person at the cashier and then enter who arrived.

Examples

Input 1

5
30 4
35 9
71 6
47 5
60 9

Output 1

30
30 47
30 71 47
71 60 47 35

Input 2

7
30 4
35 9
71 6
47 5
45 14
65 9
60 8

Output 2

30
30 47
30 71 47
71 60 47
71 65 60 47 35
60 47 35 45

Input 3

7
30 4
35 9
71 12
47 5
45 14
65 10
60 7

Output 3

30
30 47
47 60
47 60 35
60 65 35
60 71 65 35
71 65 35 45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment