-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecreio.c
108 lines (107 loc) · 2.23 KB
/
recreio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define DEBUG if(0)
//https://thehuxley.com/problem/414/code-editor/?quizId=6124
typedef struct _node
{
int person;
int priority;
struct _node* next;
}node;
typedef struct queue
{
int size;
node* head;
}Q;
Q* initQ(){
Q* new_q = (Q*) malloc(sizeof(Q));
new_q->size = 0;
new_q->head = NULL;
return new_q;
}
int isEmpty(Q* q)
{
return (q->head == NULL);
}
void priorityEnqueue(Q* q, int person, int priority)
{
node* newT = (node*) malloc(sizeof(node));
newT->person = person;
newT->priority = priority;
if ((isEmpty(q)) || (priority > q->head->priority))
{
newT->next = q->head;
q->head = newT;
//printf("primeiro caso\n");
}
else
{
node* tmp = q->head;
while ((tmp->next != NULL) && (tmp->next->priority >= priority))
{
tmp = tmp->next;
}
newT->next = tmp->next;
tmp->next = newT;
}
//printf("pessoa: [%d] | prioridade [%d]\n",person,priority);
}
void printer(Q* q)
{
node* tmp = q->head;
while (tmp != NULL)
{
printf("[%d,%d]→",tmp->priority,tmp->person);
tmp = tmp->next;
}
}
void flusher(Q* q)
{
while (q->head != NULL)
{
node* current = q->head->next;
free(q->head);
q->head = current;
}
}
int comp(Q* q, int n[], int size)
{
//printf("começo da comp\n");
int ans = 0;
int i = 0;
node* tmp = q->head;
while (tmp != NULL && i<size)
{
if (tmp->person == i)
{
//printf("[%d] != [%d]\n", tmp->person, i);
ans++;
}
i++;
tmp = tmp->next;
}
return ans;
}
int main() {
int cases,du,zi;
scanf("%d",&cases); //gets the number of tests
Q* q = initQ();
for (int i = 0; i < cases ; i++)
{
scanf("%d",&zi); //number of students
int input[zi];
for (int i = 0; i < zi; i++)
{
scanf("%d",&input[i]);
priorityEnqueue(q,i,input[i]);
DEBUG printer(q);
DEBUG printf("\n");
}
du = comp(q,input,zi);
printf("%d\n",du);
flusher(q);
}
return 0;
}